Cakephp模型链接查询问题
这是我的模型:
User hasmany Photos
Photo belongto User
非常简单的一对多关系。
在控制器中,我想查看一个用户的所有照片。
$this->User->id = $id
$this->User->Photos->read()
这不起作用,我知道如果我使用
$cond=array(
'conditions' => array('UserId =' => $id),
'recursive' => -1
);
$relationsFrom = $this->User->Photos->find('all', $cond);
Can I do this query without using find 和条件? 当我链接模型时,我给出了外键。为什么我还要再写一次? 我记得在 Ruby on Rails User.Photos 中给了我用户的所有照片。
谢谢
This are my models:
User hasmany Photos
Photo belongto User
Really simple One to many relation.
In the controller i would like to see all the Photos of one User.
$this->User->id = $id
$this->User->Photos->read()
That not working and i know that if i use
$cond=array(
'conditions' => array('UserId =' => $id),
'recursive' => -1
);
$relationsFrom = $this->User->Photos->find('all', $cond);
Can I do this query without using find with the conditions?
I give the foreign key when i've linked the models. Why have I to write again?
I remember that in Ruby on Rails User.Photos give me all the photos of the User.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用
Containable
行为?它会按照您想要的方式为您选择相关模型。它将根据您的关系选择相关字段Why don't you use
Containable
behavior? It will select the related models for you in the way that you want. It will select the related fields based on your relationships我可以告诉你为什么它会这样工作,但不能告诉你为什么它被设计成这样工作:
对象
$this->User->Photos
是Photo
模型类,对引用它的User
模型没有特殊引用。您可以使用$this->User->Photos
就像使用new Photo
一样。CakePHP 的数据库层围绕将查询编码为数组、将其转换为 SQL 并以数据数组的形式返回结果的方法。它不像某些 ORM 那样构建查询对象。
I can tell you why it works like that but not why it was designed to work like that:
The object
$this->User->Photos
is an instance of thePhoto
model class that has no special reference to theUser
model that references it. You can use$this->User->Photos
just like you could usenew Photo
.CakePHP's database layer revolves around methods that take queries encoded as arrays, translate them to SQL, and return the result as arrays of data. It does not build a query object like some ORMs do.