I have been stumped by CakePHP in how to query the DB in CakePHP and return things to $data only when the $data query table [id] has a matching [sub_id] in a second table
我被CakePHP难以理解如何在CakePHP中查询数据库并仅在$ data查询表[id]在第二个表中具有匹配的[sub_id]时将内容返回到$ data
a standard query:
标准查询:
$data = $this->Table1->findAll(array("Table1.deleted" => "0"), null, "Table1.id DESC", 25, null, 1);
But I want to only have values put into $data when the $data['Table1']['id'] is found in ['table2']['sub_id']
但是我想在['table2'] ['sub_id']中找到$ data ['Table1'] ['id']时只将值放入$ data
Thanks!
3 个解决方案
#1
If you have your relationships setup properly it should do this automatically. Can you paste your Model relationship setup for Table1 and Table2?
如果您正确设置了关系,则应自动执行此操作。你可以粘贴Table1和Table2的模型关系设置吗?
#2
Supernovah -
Please clarify one thing for me: you write that you want to only have values put into $data when table1.id is found in table2.sub_id. Do you mean that table2.sub_id is a foreign key, linking to table1?
请为我澄清一件事:你写的是当table1..sub_id中找到table1.id时你想要只将值放入$ data。你的意思是table2.sub_id是一个外键,链接到table1?
I think Beau is right -- if you have the models correctly linked, using a HABTM or belongsTo, etc., variable, the findAll should automatically pull the associated records from table2.
我认为Beau是对的 - 如果你正确地链接了模型,使用HABTM或belongsTo等变量,findAll应该自动从table2中提取相关记录。
The final caveat is that the model associations are affected by the value of Model->recursive. If you have changed the value of the recursive property in your code, it would alter how deep the model relations are allowed to go on a given query.
最后需要注意的是模型关联受Model-> recursive的影响。如果您在代码中更改了递归属性的值,则会改变允许模型关系在给定查询上的深度。
HTH!
#3
In the model, in the relation array add:
在模型中,在关系数组中添加:
$hasMany = array(
.....
'required' => true
....
);
This should make it do an inner join in sql rather than a left join. Hope this helps.
这应该使它在sql而不是左连接中进行内连接。希望这可以帮助。
#1
If you have your relationships setup properly it should do this automatically. Can you paste your Model relationship setup for Table1 and Table2?
如果您正确设置了关系,则应自动执行此操作。你可以粘贴Table1和Table2的模型关系设置吗?
#2
Supernovah -
Please clarify one thing for me: you write that you want to only have values put into $data when table1.id is found in table2.sub_id. Do you mean that table2.sub_id is a foreign key, linking to table1?
请为我澄清一件事:你写的是当table1..sub_id中找到table1.id时你想要只将值放入$ data。你的意思是table2.sub_id是一个外键,链接到table1?
I think Beau is right -- if you have the models correctly linked, using a HABTM or belongsTo, etc., variable, the findAll should automatically pull the associated records from table2.
我认为Beau是对的 - 如果你正确地链接了模型,使用HABTM或belongsTo等变量,findAll应该自动从table2中提取相关记录。
The final caveat is that the model associations are affected by the value of Model->recursive. If you have changed the value of the recursive property in your code, it would alter how deep the model relations are allowed to go on a given query.
最后需要注意的是模型关联受Model-> recursive的影响。如果您在代码中更改了递归属性的值,则会改变允许模型关系在给定查询上的深度。
HTH!
#3
In the model, in the relation array add:
在模型中,在关系数组中添加:
$hasMany = array(
.....
'required' => true
....
);
This should make it do an inner join in sql rather than a left join. Hope this helps.
这应该使它在sql而不是左连接中进行内连接。希望这可以帮助。