CakePHP:在遥远相关的模型中查找信息
我有一个新闻模型,它与艺术家模型有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你拥有的东西相当不错。我总是更喜欢使用多个查询,而不是使用创建临时表的大量联接。它会稍微降低性能。
您也可以尝试类似下面的内容
类似此查询的内容也将为您提供所需的内容(尚未检查错误)
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
Something along the likes of this query will also get you what you need (haven't error checked)