如何在另一个控制器Cakephp 2中使用loadModel和modified字段

时间:2015-03-14 00:03:03

标签: php cakephp cakephp-2.0 cakephp-2.3

我有两张桌子,车和位置;我想在LocationsController中加载loadModel Car,当我添加位置时,我希望它能自动将状态栏中的列表寄存器置于locationsController中:

    public function add($car_id) {
        if ($this->request->is('post')) {
            $this->Location->create();
            if ($this->Location->save($this->request->data)) {
                $this->Session->setFlash(__('The location has been saved.'));
                return $this->redirect(array('action' => 'index'));
                App::import('model','Car');
            $this->Car->write('Car.status', 'unavailable');
            } else {
                $this->Session->setFlash(__('The location could not be saved. Please, try again.'));
            }
        }
         $users = $this->Location->User->find('list');
        $agencies = $this->Location->Agency->find('list');
        /*$cars = $this->Location->Car->find('list');*/
        $this->set(compact('agencies'));
        $this->set('car_id', $car_id);



    }

1 个答案:

答案 0 :(得分:0)

删除此行

App::import('model','Car');
$this->Car->write('Car.status', 'unavailable');

以下代码你必须写

 if ($this->Location->save($this->request->data)) {
                $this->Session->setFlash(__('The location has been saved.'));



             $this->loadModel("Car");
             $this->Car->id=$car_id;
             $this->Car->saveField("status","unavailable");

            return $this->redirect(array('action' => 'index'));

            } else {
                $this->Session->setFlash(__('The location could not be saved. Please, try again.'));
            }

Check Manual here

相关问题