【发布时间】:2023-03-11 19:49:01
【问题描述】:
我试图在 cake php 中加入 3 个表。 我会缩短表格,我必须让它变得简单。
table_users(
id int primary key,
username varchar(10),
password varchar(10),
)
table_details(
id int primary key,
user_id int, //fk of table_users.id
//more fields here
)
table_ot(
id int primary key,
user_id int, //fk of table_users.id
//more fields here
)
我计划使用 user_id 加入 table_details 和 table_ot。 在cake bake生成的模型中,table_details是join table_users,table_ot是table_users。
但是 table_details 没有加入 table_ot。 这是table_details和table_ot的内容。
$this->belongsTo('table_users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
也试过这个在控制器里还是不行。
$Overtime = $this->table_ot->find()->all(array('joins' =>
array(
'table' => 'table_table_details',
'alias' => 'table_table_details',
'type' => 'full',
'foreignKey' => false,
'conditions'=> array('table_ot.user_id = table_table_details.user_id')
)
));
任何建议..请帮助
【问题讨论】:
-
看起来 table_details 和 table_ot 没有关系..那么你为什么需要加入他们?在 cakephp 中使用 contains 比 join 更容易。
-
@ManoharKhadka 我需要从 table_ot 中提取 table_details 中的一些字段。它们具有相同的 user_id 值。在 sql 语句中,它们可以像 table_details.user_id = table_ot.user_id 一样连接