Cakephp模型链接查询问题

发布于 2025-01-04 11:54:46 字数 570 浏览 4 评论 0原文

这是我的模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

差↓一点笑了 2025-01-11 11:54:46

为什么不使用 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

擦肩而过的背影 2025-01-11 11:54:46

我可以告诉你为什么它会这样工作,但不能告诉你为什么它被设计成这样工作:

对象 $this->User->PhotosPhoto 模型类,对引用它的 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 the Photo model class that has no special reference to the User model that references it. You can use $this->User->Photos just like you could use 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. It does not build a query object like some ORMs do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文