2

我有一个 News 模型,它与一个 Artists 模型具有 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'
    ));
4

1 回答 1

0

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)

于 2011-03-30T06:15:59.147 回答