Reputation: 59
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));
}
Upvotes: 0
Views: 13562
Reputation: 14860
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.
Upvotes: 1