Cakephp关系问题

时间:2013-06-29 21:09:10

标签: cakephp cakephp-2.0

Company class
  public $hasMany = array(
    'Person' => array(
        'className' => 'Lasso.Person',
        'foreignKey' => 'company_id',

    )



Person class
public $belongsTo = array(
                'Company' => array(
        'className' => 'Lasso.Company',
        'foreignKey' => 'id',
        'conditions' => '',
        'fields' => 'company',
        'order' => ''
    )
);

如何将People视图中的company_id字段作为公司的显示名称而不仅仅是company_id的值?

2 个答案:

答案 0 :(得分:0)

public $displayField = 'companyName';

答案 1 :(得分:0)

Person课程中,您可以写下:

public $belongTo = array(
    'Company' => array(
          'className' => 'Lesso.Company',
          'foreignKey' => 'company_id', // Here use company_id instead of id
          .....
     )
);

并且您还需要将company_id字段放在persons表中。