Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Extending an entity is the way to go. In the Doctrine2 world, they talk about inheritance mapping. Here a code example. It defines a <code>BaseEntity</code> then it is extendsed to create a <code>BaseAuditableEntity</code> and finally there is a <code>User</code> entity extending <code>BaseAuditableEntity</code>. The trick is to use the <code>@Orm\MappedSuperclass</code> annotation. This inheritance scheme will create a single table even if there is three entities in my relationships graph. This will then merge all properties into a single table. The table created will contains every property mapped through the relations, i.e. properties from BaseAuditableEntity and from User. Here the code examples:</p> <p><code>Acme\WebsiteBundle\Entity\BaseEntity.php</code></p> <pre><code>namespace Acme\WebsiteBundle\Entity; use Doctrine\ORM\Mapping as Orm; /** * @Orm\MappedSuperclass */ class BaseEntity { } </code></pre> <p><code>Acme\WebsiteBundle\Entity\BaseAuditableEntity.php</code></p> <pre><code>namespace Acme\WebsiteBundle\Entity; use Doctrine\ORM\Mapping as Orm; /** * @Orm\MappedSuperclass */ class BaseAuditableEntity extends BaseEntity { private $createdBy; /** * @Orm\Column(type="datetime", name="created_at") */ private $createdAt; /** * @Orm\ManyToOne(targetEntity="User") * @Orm\JoinColumn(name="updated_by", referencedColumnName="id") */ private $updatedBy; /** * @Orm\Column(type="datetime", name="updated_at") */ private $updatedAt; // Setters and getters here } </code></pre> <p><code>Acme\WebsiteBundle\Entity\User.php</code></p> <pre><code>namespace Acme\WebsiteBundle\Entity; use Acme\WebsiteBundle\Entity\BaseAuditableEntity; use Doctrine\ORM\Mapping as Orm; /** * @Orm\Entity(repositoryClass="Acme\WebsiteBundle\Entity\Repository\UserRepository") * @Orm\Table(name="acme_user") */ class User extends BaseAuditableEntity implements AdvancedUserInterface, \Serializable { /** * @Orm\Id * @Orm\Column(type="integer") * @Orm\GeneratedValue */ private $id; /** * @Orm\Column(type="string", name="first_name") */ private $firstName; /** * @Orm\Column(type="string", name="last_name") */ private $lastName; /** * @Orm\Column(type="string", unique="true") */ private $email; // Other properties // Constructor // Setters and getters } </code></pre> <p>Here a link to the official inheritance mapping documentation of Doctrine 2.1: <a href="http://www.doctrine-project.org/docs/orm/2.1/en/reference/inheritance-mapping.html" rel="noreferrer">here</a></p> <p>Hope this helps, don't hesitate to comment if you need more information.</p> <p>Regards,<br/> Matt</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload