如何在另一个控制器 Cakephp 2 中加载模型和修改字段

how loadModel and modified field in another controller Cakephp 2

我有两个 table,汽车和位置;我想要 LocationsController 中的 loadModel Car,当我添加位置时,我希望它自动在 Cars table 中的状态列注册到 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);



    }

删除这行

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.