简体   繁体   English

Cakephp模型链接查询问题

[英]Cakephp model linking query problems

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. 我记得在Ruby on Rails User中,照片给了我User的所有照片。

Thanks 谢谢

Why don't you use Containable behavior? 您为什么不使用可Containable行为? It will select the related models for you in the way that you want. 它将以您想要的方式为您选择相关的模型。 It will select the related fields based on your relationships 它将根据您的关系选择相关字段

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 the Photo model class that has no special reference to the User model that references it. 对象$this->User->PhotosPhoto模型类的实例,该类没有对引用它的User模型的特殊引用。 You can use $this->User->Photos just like you could use new Photo . 您可以使用$this->User->Photos ,就像可以使用new 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. CakePHP的数据库层围绕着一些方法,这些方法采用编码为数组的查询,将查询转换为SQL,然后将结果作为数据数组返回。 It does not build a query object like some ORMs do. 它不像某些ORM那样构建查询对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM