yii框架内连接
我有一类文章与表日期具有 1:N 关系。我需要显示每个日期的文章列表。我使用带有内部联接的 findBySql 。
'SELECT *
FROM articles_art as art
INNER JOIN dates_dat as dat
ON art.id_art = dat.idart_dat
WHERE art.validated_art = 1
AND dat.date_dat <= "' . $todayDate .
'" ORDER BY dat.date_dat, art.rank_art');
我不明白的是为什么当我尝试访问articles[$key]->dat时,dat是日期数组而不是对象日期? 谢谢
I have a class Articles with a 1:N relation with table Dates. I need to show a list of articles for each date. I use a findBySql with a Inner join.
'SELECT *
FROM articles_art as art
INNER JOIN dates_dat as dat
ON art.id_art = dat.idart_dat
WHERE art.validated_art = 1
AND dat.date_dat <= "' . $todayDate .
'" ORDER BY dat.date_dat, art.rank_art');
What I don't understand is why when I try to access articles[$key]->dat, dat is an array of dates and not the object date?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Yii 有一种透明的方式(无需编写 sql)来做到这一点。您可以在模型上使用范围和关系方法来获取数据。
这种方式的优点是,与 findBySql 不同,每次要查找某些内容时都需要定义一条 sql,您可以多次使用范围和关系。
因此,假设您有一个名为 Article 的模型和另一个名为 Date 的模型,您的模型将显示如下:
您的查找结果将显示 likes:
上面的代码只是一个示例,说明在 Yii 上执行此操作的正确方法,我希望对您有所帮助。
Yii has a transparent way (without writing sql) to do it. You can use scopes and relations methods on model to get data.
The advantage to this way is that unlike findBySql that you need to define a sql each time that you want find something, you can use more than one time the scopes and relations.
So assuming that you have a model called Article and another called Date, you models will shows like:
And your find will show likes:
The code above is just an example to illustrate the correct way to do it on Yii, I hope that help you.