Astaroth
Astaroth

Reputation: 783

Overwriting Doctrine entities of a Third Party Symfony Bundle

I always had this question in mind but I could always walk around the problem without facing it directly. Until today that is.

I'm having the following needs:

There's a third party bundle with a Entity that I want to overwrite so I can add some extra Annotations (Not just mapping annotations, JMS annotations, BeSimple Annotations, etc). The entity is not a Mapped Superclass nor I can seem to solve the problem by using Interfaces.

Can someone shred some light on the problem?

I'll explain with code:

Third party Bundle Entity Class I want to extend someway:

namespace Third\PartyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * User
 *
 * @ORM\Table(name="User")
 * @ORM\Entity(repositoryClass="Third\PartyBundle\Repository\UserRepository")
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=20
     *
     * @Assert\NotBlank()
     */
    private $name;
}

What I want to achieve:

namespace My\OwnBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap;

/**
 * User
 *
 * @ORM\Table(name="User")
 * @ORM\Entity(repositoryClass="My\OwnBundle\Repository\UserRepository")
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @Soap\ComplexType("int", nillable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=20
     * @Soap\ComplexType("string", nillable=false)
     * @Assert\NotBlank()
     */
    private $name;
}

Notice that the second class has extra Annotations from Besimple.

Upvotes: 0

Views: 564

Answers (2)

Wilt
Wilt

Reputation: 44356

In most bundles that ship entities there is a possibility to set your custom entity classes to use in the config. Did you check for such possibility?

An example on such a library/module is the ZfcUser module from ZF-Commons. In that module there is a global key 'user_entity_class' in the global config file that defaults to ZfcUser\Entity\User but can be used to point to an alternative entity class to use.

Check the config file here for reference.

Upvotes: 0

ilyass
ilyass

Reputation: 402

You can use the Bundle inheritance of symfony, like :

    class BorhUserBundle extends Bundle
    {
       public function getParent()
       {
          return 'FOSUserBundle';
       }
    }

Here is the documentation : symfony doc

So you just need to create entities in the same directory with the same name, and inherits the entities... This is how we extends the FosUserBundle

Upvotes: 1

Related Questions