kero_zen
kero_zen

Reputation: 694

Reuse method in Doctrine Entity

I have 3 entities in Symfony 2 framework :

These entities have one common property cause they have ManyToMany relation :

 /**
 * @ORM\ManyToMany(targetEntity="Fccq\CoreBundle\Entity\Tag", cascade={"persist"})
 */
private $tags;

And some methods :

/**
 * @param array $tags
 */
public function addListTags($tags)
{
    foreach($tags as $tag)
    {
        $this->addTag($tag);
    }
}

/**
 * @param Tag $tag
 */
public function addTag(\Fccq\CoreBundle\Entity\Tag $tag)
{
    if (!$this->tags->contains($tag)) {
        $this->tags->add($tag);
    }
}

/**
 * @param Tag $tag
 */
public function removeTag(\Fccq\CoreBundle\Entity\Tag $tag)
{
    $this->tags->removeElement($tag);
}

I don't want this code duplication so I start to implement "traits". Works fine but it's not supported by php 5.3 (my production environment). Simple inheritance is not a good solution because my User entity already inherit from another class.

Any ideas ? Thx

Upvotes: 1

Views: 117

Answers (1)

zeykzso
zeykzso

Reputation: 493

What if you still use inheritence, put these functions in a base tag class, and extend it by your opportunity, company, and by the class the user extends? I don't know other solutions, as you can't use helper functions in entity classes (or at least it is not recommended). Maybe someone else still has some ideas..

Upvotes: 2

Related Questions