CakePHP:在遥远相关的模型中查找信息

发布于 2024-10-27 20:16:44 字数 602 浏览 4 评论 0原文

我有一个新闻模型,它与艺术家模型有 HABTM 关系,而艺术家又拥有许多巡演日期。

如果我想查找与当前新闻项目相关的所有巡演日期,对于 CakePHP 来说,有效的表达方式是什么?

这就是我到目前为止所拥有的;我想知道(a)它看起来是否应该工作,以及(b)是否有更简洁的编写方式:

    $relatedartists = $this->News->ArtistsNews->find('list', array(
        'conditions'=>array('ArtistsNews.news_id' => $id),
        'fields'=>array('artist_id')
    ));
    $livedates = $this->News->Artists->Tour->find('all', array(
        'conditions'=>array('Tour.artist_id'=> $relatedartists, 
            'date >= ' . time()),
        'order'=>'date ASC'
    ));

I have a News model, which has a HABTM relationship with an Artists model, and an artist in turn hasMany tourdates.

If I want to find all tourdates related to the current news item, what is an efficient way of phrasing that for CakePHP?

This is what I have so far; I'm wondering if (a) it looks like it should work, and (b) if there's any more concise way of writing it:

    $relatedartists = $this->News->ArtistsNews->find('list', array(
        'conditions'=>array('ArtistsNews.news_id' => $id),
        'fields'=>array('artist_id')
    ));
    $livedates = $this->News->Artists->Tour->find('all', array(
        'conditions'=>array('Tour.artist_id'=> $relatedartists, 
            'date >= ' . time()),
        'order'=>'date ASC'
    ));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

黑白记忆 2024-11-03 20:16:44

你拥有的东西相当不错。我总是更喜欢使用多个查询,而不是使用创建临时表的大量联接。它会稍微降低性能。

您也可以尝试类似下面的内容

$opts = array(
  'conditions' => array(
     'ArtistsNews.news_id' => $id
   )
);
$this->News->Artists->recursive = 2;
$this->News->Artists->find('all', $opts);

类似此查询的内容也将为您提供所需的内容(尚未检查错误)

What you have is pretty good. I always prefer to use multiple queries rather than use massive joins which create temporary tables. It can reduce performance somewhat.

You might also try something like the below

$opts = array(
  'conditions' => array(
     'ArtistsNews.news_id' => $id
   )
);
$this->News->Artists->recursive = 2;
$this->News->Artists->find('all', $opts);

Something along the likes of this query will also get you what you need (haven't error checked)

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