Sql 在cakephp中连接与主表不直接相关的表
我有三个表,其结构类似于以下内容:Sql 在cakephp中连接与主表不直接相关的表,sql,cakephp,has-one,Sql,Cakephp,Has One,我有三个表,其结构类似于以下内容: employees offices postings ________ ________ ___________ id id employees_id name name offices_id 所以我想知道如何从员工模型中获取办公室名称。将office放入$hasOne数组中,在“on子句”中显示未知列“office.employees\u id”。我应该怎么做才能在结果中获得办公
employees offices postings
________ ________ ___________
id id employees_id
name name offices_id
所以我想知道如何从员工模型中获取办公室名称。将
office
放入$hasOne
数组中,在“on子句”中显示未知列“office.employees\u id”
。我应该怎么做才能在结果中获得办公室名称?如果您想继续使用数据库,您可以使用has和allown多个关系。
在employee.php中:
public $hasAndBelongsToMany = [
'Office'=> [
'foreign_key' => 'employees_id',
'joinTable' => 'postings',
'associationForeignKey' => 'offices_id',
]
];
然后,您可以从员工模型中获取office
我真正想建议的是在employees
表中放置一列office\u id
,然后
$public $belongsTo = ['office'];
在您的员工模型中
PS:PHP5.4或更新版本支持方括号[]
,如果您使用的是PHP5.3或更低版本,可能需要将其替换为array()