簡體   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