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 comprises 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;

 /**
  * ORM Entity Document.
  */
 #[ORM\Entity]
 #[ORM\Table(name: 'acme_demo_document')]
 class Document
 {
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\Column(
        name: 'subject',
        type: 'string',
        length: 255,
        nullable: false
    )]
    private $subject;

    #[ORM\Column(
        name: 'description',
        type: 'string',
        length: 255,
        nullable: false
    )]
    private $description;

    #[ORM\Column(
        name: 'due_date',
        type: 'datetime',
        nullable: true
    )]
    private $dueDate;

    #[ORM\ManyToOne(targetEntity: 'Acme\Bundle\DemoBundle\Entity\Priority')]
    #[ORM\JoinColumn(name: 'priority_id', nullable: true, onDelete: 'SET NULL')]
    private $priority;

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

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    public function setDueDate(?\DateTimeInterface $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;

 /**
  * ORM Entity Priority.
  */
 #[ORM\Entity]
 #[ORM\Table(name: 'acme_demo_priority')]
 class Priority
 {
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\Column(
       name: 'label',
       type: 'string',
       length: 255,
       nullable: false
    )]
    private $label;

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

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

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

      return $this;
    }
 }

What’s next