Doctrine 2 Entities: Should They Contain Logic?

The site of the doctrine is omitted, so I am looking for information here:

It is supposed to contain Doctrine 2 objects:

  • properties and recipients and setters
  • properties, getters / seters and domain logic

thank

+5
source share
2 answers

Some domain logic is beautiful if applied to the entity itself. For example, the following things are in order:

class myEntity {
  // ...

  /**
   * @OneToMany(targetEntity="LineItem")
   */ 
  protected $items;

  public function equals($otherEntity){
     //compare $this->lineItems and $otherEntity->lineItems, and return true if
     //they are identical      
  }

  /**
   * More business logic internal to an entity.
   */
  public function subtotal(){
    $total = 0;
    foreach($this->items as $i) $total += $i;
    return $i;
  }
}

What you do not want in entites are things with side effects outside this object (or its entities), data persistence (entities should never know about the EntityManager or repositories, etc.).

, , - ( Entity). - , , .

+10

, -. . @timdev, 100% . EntityManager, Repositories Services; .

, .

+2

All Articles