Doctrine2 Best Practice, Should Organizations Use the Services?

I asked a similar question a while ago: Using the Data Mapper template, if objects (domain objects) know about Mapper? However, this was common and I am really interested in how to do a few things with Doctrine2 specifically .

Here, a simple exemplary model: each Thingcan have Voteon User, a Usercan distinguish more than one Vote, but only the last Vote. Since other data ( Msssageetc.) is related to Vote, when the second is posted Vote, the original Voteone cannot just be updated, it needs to be replaced.

Currently Thinghas this function:

public function addVote($vote)
{
  $vote->entity = $this;
}

And Votesets up the relationship:

public function setThing(Model_Thing $thing)
{
  $this->thing = $thing;
  $thing->votes[] = $this;
} 

It seems to me that to ensure Useronly the latter, Voteit is considered that what Thingshould provide, and not some kind of service layer .

So, to save this in the Model, a new feature Thing:

public function addVote($vote)
{
  foreach($this->votes as $v){
    if($v->user === $vote->user){
      //remove vote
    }
  }
  $vote->entity = $this;
}

So how do I remove Votethe domain model from the inside? . Should I relax Vote::setThing()to accept NULL? Should I include some level of service that I Thingcan use to remove votes? When the accumulation of votes begins, which foreachwill be slow - should the service layer be used so that it Thingcan search Votewithout loading the entire collection?

; Doctrine2, ?

+3
2

. , . EntityManager , , O (n) , ( lame , DQL).

, , -, . O'Phinney .

, , Entities , . , Entity . , - , , .

, :

public function addVote($vote)
{
   $this->_service->addVoteToThing($vote, $thing);
}

, Doctrine - , .

+7

EntityRepository, :

class BlogPostRepository extends EntityRepository implements IBlogPostRepository {}

EntityManager.

+6

Source: https://habr.com/ru/post/1780761/


All Articles