0

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));

}
3
  • That line should be $items = .... not $items[] = .... Commented Jun 29, 2014 at 16:55
  • That works now thanks. Could you explain why it wasn't working? I can't push a two-dimensional array of objects onto an existing array? Commented Jun 29, 2014 at 17:28
  • @user3784836 you can add results to an array, but you'd have to use the "+" operator. By doing $items[] = ..., you end up adding a single element to the array, and that element is an array itself, containing the found records. Commented Jul 10, 2014 at 13:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.