Search code examples
phpdoctrine-ormdoctrine

Doctrine return only one parameter of the related entity


Entity TestEntity has parameter which relates to another entity called TestEntityRelated. This entity TestEntityRelated has only one integer parameter - price. I want this price parameter to be returned as single parameter instead of the whole invalidPrice object.

TestEntity entity:

/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer", options={"unsigned":true})
 */
 private $id;

 /**
 * @ORM\OneToOne(targetEntity=TestEntityRelated::class, mappedBy="offer", cascade={"persist", "remove"})
 */
private $price;

TestEntityRelated entity:

 /**
 * @ORM\Id
 * @ORM\OneToOne(targetEntity=TestEntity::class, inversedBy="price")
 * @ORM\JoinColumn(name="id", referencedColumnName="id")
 */
private $entity;

/**
 * @ORM\Column(type="integer", options={"unsigned":true})
 */
private $price;

For example if I get the Entity object it should contain this:

-price: 1

instead of:

-price: App\Entity\TestEntityRelated {#1551
-entity: App\Entity\Entity {#1532}
-price: 1
}

Solution

  • Currently solved this by editing the getPrice getter:

    public function getPrice(): ?int
    {
        return $this->price ? $this->price->getPrice(): null;
    }
    

    I return the price property instead of the whole price object.