CakePHP 2 请帮助模型协会
Posted
技术标签:
【中文标题】CakePHP 2 请帮助模型协会【英文标题】:CakePHP 2 Please help on Model Association 【发布时间】:2013-04-23 06:06:49 【问题描述】:假设我有 3 个模型。
-
牙医(角色为“D”的用户)
申请人(有关每个用户的更多详细信息,例如地址、电话、兴趣位置等)
职位(在申请表中我只存储 position_id,这是描述的地方)
类牙医扩展 AppModel public $hasOne = '申请人';
类申请人扩展 AppModel public $belongsTo = array('牙医', '位置');
类位置扩展 AppModel
当我在我的 DentistsController 中使用 $this->Dentist->find('all');
时,牙医的观点有问题,因为 SQL 就像
select *
from dentists left outer join applicants
on dentists.id = applicants.dentist_id
不再像左外连接位置...
但如果我在申请人控制器中使用$this->Applicant->find('all');
,我会得到左外连接位置...
如何设置模型关联以从我的 DentistsController 获取连接语句到表“位置”。
谢谢大家。
【问题讨论】:
【参考方案1】: Your models should have following association
Positions model:
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Applicant' => array(
'className' => 'Applicant',
'foreignKey' => 'position_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
Application model:
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Dentist' => array(
'className' => 'Dentist',
'foreignKey' => 'dentist_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Dentist' => array(
'className' => 'Dentist',
'foreignKey' => 'application_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
Dentist model:
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Applicant' => array(
'className' => 'Applicant',
'foreignKey' => 'applicant_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Applicant' => array(
'className' => 'Applicant',
'foreignKey' => 'dentist_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
【讨论】:
以上是关于CakePHP 2 请帮助模型协会的主要内容,如果未能解决你的问题,请参考以下文章