检索关系时是否可以添加 WHERE 子句?

发布于 2024-09-08 13:53:54 字数 615 浏览 4 评论 0原文

从理论上讲,在获取与关系对应的对象的属性时是否可以添加 WHERE 子句?

就概念而言,假设我只想检索过去 5 天内发表的前 3 篇博客文章。我的“博客”对象有一个“帖子”属性,它被定义为关系。

更新...

由于有些人在理解我所说的关系的含义时遇到一些困难:

class Blog extends Doctrine_Record {

       ...

       public function setUp() {

            $this->hasMany("Note as Posts", array(
                "local" => "blog_name",
                "foreign" => "post_id",
                "refClass" => "BlogPost"
            ));

       }
}

如您所见,这是一种受教义支持的明确关系。当我使用它查询时:

     $instanceOfBlog->Posts...........

我想知道当时是否可以添加额外的子句。

In doctrine, is it possible to add a WHERE clause when fetching a property of an object that corresponds to a relationship?

In terms of concept, let's say I want to retrieve only the first 3 blog posts made in the last 5 days. My "blog" object has a "posts" property which is defined as a relationship.

Update...

As some people are having some difficulties understanding what I mean by a relationship:

class Blog extends Doctrine_Record {

       ...

       public function setUp() {

            $this->hasMany("Note as Posts", array(
                "local" => "blog_name",
                "foreign" => "post_id",
                "refClass" => "BlogPost"
            ));

       }
}

As you can see, this is an explicit relationship as supported by doctrine. When I query using it:

     $instanceOfBlog->Posts...........

I'd like to know if I can add additional clauses at that time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

吃素的狼 2024-09-15 13:53:54

不确定我是否明白你的意思,但如果这就是我在你的 BlogTable 课程中的想法:

public function getRecentPosts()
{
  $qry = self::createQuery("b")
    ->innerJoin("b.Posts p")
    ->where("p.created_at > ?", date("Y-m-d H:i:s", strtotime("-5 days")))
    ->orderBy("p.created_at DESC")
    ->limit(3);

  $results = $qry->execute();
}

这就是你所追求的吗?这基于 Posts 对象中的 created_at 字段,并假设在 BlogPosts 表之间定义了关系。

然而我可能完全误解了你的问题:-)

Not sure I follow you, but if it's what I think then in your BlogTable class:

public function getRecentPosts()
{
  $qry = self::createQuery("b")
    ->innerJoin("b.Posts p")
    ->where("p.created_at > ?", date("Y-m-d H:i:s", strtotime("-5 days")))
    ->orderBy("p.created_at DESC")
    ->limit(3);

  $results = $qry->execute();
}

Is that what you were after? This is based on the created_at field in the Posts object, and assumes a relationship is defined between the Blog and Posts tables.

I may have misunderstood your question entirely however :-)

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