开发者

Finding Related Rows in Doctrine -- "Cleanest" Design?

开发者 https://www.devze.com 2023-02-11 06:11 出处:网络
I\'m new to Doctrine, and I\'m trying to get my head around both Doctrine and Symfony at the same time, 开发者_如何学JAVAso please bear with me.

I'm new to Doctrine, and I'm trying to get my head around both Doctrine and Symfony at the same time, 开发者_如何学JAVAso please bear with me.

I have a "Sentence" object. Sentences can be "rated" (scored out of five) by Users. I'm modelling this with the fairly obvious design where I have a Sentence table, a User table and a Rating table. Each row in the Rating table represents one user's rating of one sentence.

Rating:
  ...
  columns:
    rating: { type: integer } # This is my score out of five
    sentence_id: { type: integer }
    user_id: { type: integer }
  relations:
    sfGuardUser: { local: user_id }
    Sentence: { local: sentence_id, onDelete: CASCADE, foreignAlias: Ratings }

That's working fine, but I'm struggling to find the cleanest way of asking questions of my model.

For example, in my code I have a logged-in User, related to my session, and I'm displaying a page representing a Sentence, which is also already sitting in a variable. I want to display the logged-in user's rating of that Sentence.

In raw SQL, effectively what I want to do is:

SELECT 
    rating 
FROM
    rating_table 
WHERE 
    sentence_id = {$sentence->id} AND user_id = {$user->id}

Now, I know I could just do this on the RatingTable, simply by writing a new query with a bit of DQL, or even using one of the "magic" finders. But that seems a bit clumsy and not very OO, when I already have a Sentence object in memory with a getRatings() method generated by Doctrine for me which must already have the rating I want in its Collection of "all ratings for this sentence".

Is there an easy, efficient way that I'm missing of doing something like $sentence->getRatings()->findByUserId($user->getId())?

Or is it actually sensible just to ignore the fact that I've already got a collection of "ratings for this sentence" in memory and dive back out to the database, ignoring them completely?


There isnt a finder within a collection like that that im aware of (unlike with Propel)... if there is and I missed it in the Doctrine API then im going to leanr a blissful piece of code when someone posts it :-)

Having said that, IF you already have the collection loaded i would just add a custom method tot he model.. for example:

public function getUserRating($user){
  if($user instanceof Doctrine_Record){ // or sfGuardUser depending on how tightly you want to couple
     $user = $user->getId();
  }

  foreach($this->getRatings() as $rating){
     if($user == $rating->getUserId()){
        return $rating;
     }
  }

  return null;
}
0

精彩评论

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