开发者

Is it possible to add WHERE clauses when retrieving relationships?

开发者 https://www.devze.com 2023-01-06 03:08 出处:网络
In doctrine, is it possible to add a WHERE clause when fetching a property of an object that corresponds to a relationship?

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 c开发者_Python百科lauses at that time.


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 :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消