CakePHP 中的 $data findAll 查询检查第二个表中的值
我一直被 CakePHP 难住了,如何在 CakePHP 中查询数据库,并仅当 $data 查询表 [id] 在第二个表中具有匹配的 [sub_id]
标准查询时才将内容返回到 $data:
$data = $this->Table1->findAll(array("Table1.deleted" => "0"), null, "Table1.id DESC", 25, null, 1);
但我只想有当在 ['table2']['sub_id'] 中找到 $data['Table1']['id'] 时,将值放入 $data
谢谢!
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
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']
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的关系设置正确,它应该会自动执行此操作。 您可以粘贴 Table1 和 Table2 的模型关系设置吗?
If you have your relationships setup properly it should do this automatically. Can you paste your Model relationship setup for Table1 and Table2?
Supernovah -
请为我澄清一件事:您写道,当在 table2.sub_id 中找到 table1.id 时,您只想将值放入 $data 中。 您的意思是 table2.sub_id 是链接到 table1 的外键吗?
我认为 Beau 是对的——如果您使用 HABTM 或 ownTo 等变量正确链接了模型,则 findAll 应该自动从 table2 中提取关联的记录。
最后需要注意的是,模型关联受到 Model->recursive 值的影响。 如果您更改了代码中递归属性的值,它将改变给定查询允许模型关系进行的深度。
哈!
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?
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.
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.
HTH!
在模型中,在关系数组中添加:
这应该使其在 sql 中执行内部联接而不是左联接。 希望这可以帮助。
In the model, in the relation array add:
This should make it do an inner join in sql rather than a left join. Hope this helps.