Create Entities

Define Entities

When working with OroPlatform, creating entities and mapping them to the database is no different from doing the same in a common Symfony application. Once you have created your bundle, create the entity classes you need in the bundle’s Entity namespace, add all the required properties, and add the required mapping annotations as usual.

A document is composed of a brief subject, a more verbose description, a due date, and a priority. Also, each document is identified by a unique identifier that is automatically generated by the database:

src/Acme/Bundle/DemoBundle/Entity/Document.php;
 namespace Acme\Bundle\DemoBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;

 /**
  * Document entity
  *
  * @ORM\Entity()
  * @ORM\Table(name="acme_document")
  */
 class Document
 {
     /**
      * @var int
      *
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      * @ORM\Column(name="id", type="integer")
      */

     protected $id;

     /**
      * @ORM\Column(name="subject", type="string", length=255, nullable=false)
      *
      * @var string
      */
     protected $subject;

     /**
      * @ORM\Column(name="description", type="string", length=255, nullable=false)
      *
      * @var string
      */
     protected $description;

     /**
      * @ORM\Column(name="due_date", type="datetime")
      *
      * @var \DateTime
      */
     protected $dueDate;

     /**
      * @ORM\ManyToOne(targetEntity="Priority")
      * @ORM\JoinColumn(name="document_priority_id", onDelete="SET NULL")
      *
      * @var Priority
      */
     protected $priority;

     public function getId(): ?int
     {
         return $this->id;
     }

     public function getSubject(): ?string
     {
         return $this->subject;
     }

     public function setSubject($subject): self
     {
         $this->subject = $subject;

         return $this;
     }

     public function getDescription(): ?string
     {
         return $this->description;
     }

     public function setDescription($description): self
     {
         $this->description = $description;

         return $this;
     }

     public function getDueDate(): ?\DateTime
     {
         return $this->dueDate;
     }

     public function setDueDate(\DateTime $dueDate): self
     {
         $this->dueDate = $dueDate;

         return $this;
     }

     public function getPriority(): ?Priority
     {
         return $this->priority;
     }

     public function setPriority(Priority $priority): self
     {
         $this->priority = $priority;

         return $this;
     }
 }

Users should be able to create and change priorities through the user interface, therefore, they are modeled as separate entities:

src/Acme/Bundle/DemoBundle/Entity/Priority.php
 namespace Acme\Bundle\DemoBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;

 /**
  * Priority entity
  *
  * @ORM\Entity()
  * @ORM\Table(name="acme_document_priority")
  */
 class Priority
 {
     /**
      * @var int
      *
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      * @ORM\Column(name="id", type="integer")
      */
     protected $id;

     /**
      * @ORM\Column(name="label", type="string", unique=true)
      *
      * @var string
      */
     protected $label;

     public function getId(): ?int
     {
         return $this->id;
     }

     public function getLabel(): ?string
     {
         return $this->label;
     }

     public function setLabel($label): self
     {
         $this->label = $label;

         return $this;
     }
 }