Correct code placement to register entity specific warnings in symfony 2

My question is about the correct and intended use of entities, repositories and logical code in symfony 2.

I have some logic specifically for the entity and therefore I put the logic code in this file. 1 thing I would like to do is log certain warnings or debug information that comes up with this object.

for example these functions are in my essence:

 * @return ArrayCollection|Humanoid[]
public function getHumanoidPrimary()
    $humanoid = $this->getHumanoidByPersonType(Humanoid::TYPE_PRIMARY);
    if ($humanoid->count() > 1){

        //This is what I was planning to do
        $this->logger->warn('you should not have more than 1 primary.  there is a problem');

    return $humanoid->first();

 * @param $personType
 * @return Humanoid[]|null|ArrayCollection
private function getHumanoidByPersonType($personType)
    //get the current step from the collection
    $criteria = Criteria::create()->where(Criteria::expr()->eq("personType", $personType));
    $humanoidsByType = $this->humanoids->matching($criteria);
    if ($humanoidsByType instanceof Humanoid) {
        return $humanoidsByType;
    } else {
        //todo: figure out how to log this warning.
        throw new \LogicException("No humanoid of personType:$personType Stored in property");


Most of the documentation I read says that the entity code should not know anything other than itself and other objects.

So here are my questions:

  • How can you log warnings in symfony 2?
  • Is this the right place for this code?
  • Should this code be moved to another class? Should there be a service?
  • Moving this to the repository doesn't seem right. repositories are for more complex data queries, not logic. It is right?

These are noob questions, but it seems that I am struggling with the code and not using it correctly.


source to share

2 answers

This is an interesting question because after almost two years of developing symfony2, I needed to stop thinking about an acceptable answer.


  • The correct way to register a warning in symfony2 is $this->logger->warning

    , so you were close to the correct solution.
  • If you want to follow the symfony2 guidelines, you should never insert a piece of code into an entity class. I've never seen this in 3-part packages, so I suppose it's good practice to avoid including this code in the entity.
  • Usually for such tasks I create an EntityNameManager (so in this case HumanoidManager

    ), which I basically create as a service and use it along with the entity
  • Yes, only db related code should be placed in repos.

Side questions

  • Don't be afraid of the number of assistants / managers. If you know that some functionality can be implemented in a superclass, do so. Otherwise, I don't think this should be a problem (think of a repository: each entity can (and MUST) have its own if necessary). In addition, the services are not named (instatiated) at any time before you use them. Of course, I cannot estimate the processing time of the xml or yml file, if this file increases, it might be interesting.
  • Ok, I've seen these packages are used to place managers in the Entity folder itself. Otherwise, you can do whatever you want: in that case, the naming convention (like declaring a namespace, etc., of course) doesn't matter. If you want to decouple the Entity from its manager BundleName / Manager might be ok.
  • Personally, I use to enter the log into managers.


Good question. My opinion:
  • It is bad practice to have a registrar in an Entity declaration: how do you pass it? Typically the entity does not use any service (it also registers).

2/3/4. The entity knows nothing about business logic. The repository is for querying business logic. If the business logic can grow and / or interact with other system boundaries, it is best to start with maintenance.

Hope for this help



All Articles