Cakephp显示第二个模型关联名称而不是ID

时间:2013-02-22 17:12:42

标签: cakephp

我有数据库中的下一个表:

  • 提升
  • 车辆
  • 制造商

提升所属车辆

车辆所属制造商

制造商拥有多辆车

LiftsController.php添加方法:

public function add() {
    if ($this->request->is('post')) {
        $this->Lift->create();
        $this->request->data['Lift']['user_id'] = $this->Auth->user('id');
        if ($this->Lift->save($this->request->data)) {
            $this->Session->setFlash(__('The lift has been saved'));
            $this->redirect(array('action' => 'my_lifts'));
        } else {
            $this->Session->setFlash(__('The lift could not be saved. Please, try again.'));
        }
    }
    $vehicles = $this->Lift->Vehicle->find('all', array('fields' => array('id', 'model', 'manufacturer_id')));
    $towns = $this->Lift->TownFrom->find('list', array('order' => 'county ASC, name ASC', 'fields' => array('id', 'name', 'county')));
    $this->set(compact('users', 'vehicles', 'towns'));
}

LiftsController.php添加视图:

    echo $this->Form->input('vehicle_id');

A printcreen:

img

所以我需要的是显示而不是制造商ID的制造商名称。 在每个模型中,$ displayField都设置为正确的值

2 个答案:

答案 0 :(得分:0)

尝试更改:

echo $this->Form->input('vehicle_id');

echo $this->Form->input('Vehicle');

答案 1 :(得分:0)

更改

$vehicles = $this->Lift->Vehicle->find('all', array('fields' => array('id', 'model', 'manufacturer_id')));

$vehicles = $this->Lift->Vehicle->find('list', array('conditions' => array('Vehicle.user_id' => $this->Auth->user('id')), 'recursive' => 0, 'fields' => array('Vehicle.id', 'Vehicle.model', 'Manufacturer.title')));