CakePhp:建模递归关联并查找
我在 CakePhp 上的模型上使用 find() 时遇到了一些问题。
我有三个以这种方式相关的模型:
Project(some_fields, item_id) ------属于----->项目(某些字段,项目 ID) ------属于-----> User(some_fieldscampi,nickname)
我需要执行 find() 并检索项目中的所有字段、项目中的字段和用户中的昵称字段。这是我的代码:
$this->set('projects', $this->Project->find('all', array('recursive' => 2)));
但我的输出不包含用户对象。 我尝试过 Containable 行为,但输出是相同的。什么坏了?
非常非常感谢
彼得
I've some trouble with a find() on a model on CakePhp.
I have three model relationed in this way:
Project(some_fields, item_id)
------belongsTo-----> Item(some_fields, item_id)
------belongsTo-----> User(some_fields campi, nickname)
I need to do a find() and retrieve all fields from project, a field from Item and the nickname field from User. This is my code:
$this->set('projects', $this->Project->find('all', array('recursive' => 2)));
but my output doesn't contains the user object.
I've tried with Containable behaviour but the output is the same. What is broken?
Many many Thanks
Peter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的方法会产生太多查询。您可能希望通过绑定和取消绑定模型来减少它们的数量。请参阅此处了解详细信息。
Your approach generates too many queries. You may want to reduce their quantity by binding and unbinding models. See details here.
正确设置您的模型或尝试加入
Set Your model properly or try with join
Project.php 模型文件应如下所示
Item.php 模型文件应如下所示
User.php 模型文件应如下所示
您可以在控制器中尝试使用以下代码。
Project.php Model file should be as below
Item.php Model file should be as below
User.php Model file should be as below
You can try with below code in the controller.