学说:加入一对一关系
假设我们有两个模型:Post
和 ViewsCount
。关系类型为 1:1。
现在我想检索最后 5 个帖子及其浏览统计数据:
$posts = PostTable::getInstance()->createQuery('p')
->leftJoin('p.ViewsCount') // relation name is "ViewsCount"
->orderBy('p.created_at DESC')
->limit(5)
->execute();
但是,没有运气。它抛出一个错误。如果我删除加入 - 一切都好。
所以,我的问题是 - 如何在 Doctrine 中自动加入/检索一对一关系以避免大量额外查询?
Suppose we have two models: Post
and ViewsCount
. Relation type is 1:1.
Now I want to retrieve last 5 posts with their views stats:
$posts = PostTable::getInstance()->createQuery('p')
->leftJoin('p.ViewsCount') // relation name is "ViewsCount"
->orderBy('p.created_at DESC')
->limit(5)
->execute();
But, there is no luck. It throws an error. If I remove joining - everything is ok.
So, my question is - How to automatically join/retrieve one-to-one relation in Doctrine to avoid lot's of additional queries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有语法错误。您还需要告诉您想要检索 ViewCount 字段:
You have an error in syntax. You also need to tell doctrine you want to retrieve the ViewCount fields:
看来你应该正确定义关系。
对于每个模型,定义与类型键(type:one)的关系。
Is seems that you should define relations properly.
For each model define relations with type key (type:one).