简体   繁体   中英

Yii Array to string conversion

I am getting an Error 500 Array to String conversion message which i can't seem to resolve. I get it from inserting the following line of code and also If I put the line of code in my update function it runs.

 $items[] = BookingRoom::model()->findAll('bookingId=:bookingId', array(':bookingId'=>1)); 
public function getItemsToUpdate(){
        // create an empty list of records
        $items = array();


            // Iterate over each item from the submitted form                        
            if (isset($_POST['BookingRoom']) && is_array($_POST['BookingRoom'])) {  

                foreach ($_POST['BookingRoom'] as $item) {
                    // If item id is available, read the record from database 
                    if ( array_key_exists('id', $item) ){

                        $items[] = BookingRoom::model()->findByPk($item['id']);
                    }
                    // Otherwise create a new record
                    else {

                        $items[] = new BookingRoom();
                    }
                } 
            } else {


                $items[] = BookingRoom::model()->findAll('bookingId=:bookingId', array(':bookingId'=>1));
            } 
            return $items;                     
    }

Update function in same Booking controller:

public function actionUpdate($id)
{
    $model=$this->loadModel($id);
            $items = array();
            $items=$this->getItemsToUpdate();




    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);                                                                              


            if(isset($_POST['BookingRoom']))
              {

                $valid=true;
                foreach($items as $i=>$item)
                {
                    if(isset($_POST['BookingRoom'][$i]))
                    $item->attributes=$_POST['BookingRoom'][$i];
                    $valid=$item->validate() && $valid;
                }

                $valid=$model->validate() && $valid;
                if($valid){

                }                                                                          
              }

    $this->render('update',
                    array('items'=>$items, 'model'=>$model));

}

That line should be $items = ... not $items[] = ... . The latter appends an element to the array. findAll() returns an array of models therefore the former should be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM