繁体   English   中英

Yii数组到字符串的转换

[英]Yii Array to string conversion

我收到错误500数组到字符串的转换消息,我似乎无法解决。 我是从插入下面的代码行中得到的,如果将代码行放入我的更新函数中,它也会运行。

 $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;                     
    }

同一预订控制器中的更新功能:

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

}

那行应该是$items = ...而不是$items[] = ... 后者将元素添加到数组。 findAll()返回一个模型数组,因此应使用前者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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