views:

49

answers:

1

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.

+3  A: 

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

richsage