学说:加入一对一关系

发布于 2024-10-08 06:07:21 字数 440 浏览 0 评论 0原文

假设我们有两个模型:PostViewsCount。关系类型为 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 技术交流群。

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

发布评论

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

评论(2

人心善变 2024-10-15 06:07:21

你有语法错误。您还需要告诉您想要检索 ViewCount 字段:

$posts = PostTable::getInstance()->createQuery('p')
  ->select('p.*, vc.*')
  ->leftJoin('p.ViewsCount vc')
  ->orderBy('p.created_at DESC')
  ->limit(5)
  ->execute();

You have an error in syntax. You also need to tell doctrine you want to retrieve the ViewCount fields:

$posts = PostTable::getInstance()->createQuery('p')
  ->select('p.*, vc.*')
  ->leftJoin('p.ViewsCount vc')
  ->orderBy('p.created_at DESC')
  ->limit(5)
  ->execute();
烂柯人 2024-10-15 06:07:21

看来你应该正确定义关系。
对于每个模型,定义与类型键(type:one)的关系。

Is seems that you should define relations properly.
For each model define relations with type key (type:one).

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