-1

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

Offer entity:

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

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

OfferInvalidPrice entity:

 /**
 * @ORM\Id
 * @ORM\OneToOne(targetEntity=Offer::class, inversedBy="invalidPrice")
 * @ORM\JoinColumn(name="offer_id", referencedColumnName="id")
 */
private $offer;

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

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

-invalidPrice: 1

instead of:

-invalidPrice: App\Entity\OfferInvalidPrice {#1551
-offer: App\Entity\Offer {#1532}
-hasInvalidPrice: 1
}
The50
  • 1,096
  • 2
  • 23
  • 47
  • If you've got a OneToOne relationship where one side only contains a single property, it sounds like the "correct" answer would be to move the property to the main Offer entity itself. If that's not possible, you can just add a getter to the Offer entity that returns `$this->invalidPrice->hasInvalidPrice` as an integer, and the rest of your application doesn't need to know that another object was involved at all. – iainn Sep 13 '21 at 14:23
  • Yes, moving that property to the main Offer entity is not possible, because it has to be on different database table. And indeed I have the hasInvalidPrice property getter, but if I get the whole Offer object it automatically returns full invalidPrice object as a property too. – The50 Sep 13 '21 at 14:29
  • Sure, and I'm not sure there's a way around that in Doctrine. But why is that causing a problem? The invalidPrice object is private - the rest of your application can't access it. I would just change the hasInvalidPrice property getter to return the related property instead (or I guess ideally a boolean?), and ignore what's happening underneath. You're working around an issue with the schema that you can't change, but the only place in your code that needs to know about that is the Offer entity. Everything else will look correct. – iainn Sep 13 '21 at 14:38
  • I just thought that it may be more cleaner way to get the data in Doctrine way. I don't need the whole object if it has only one property. – The50 Sep 13 '21 at 14:45
  • Does this answer your question? [How to get a one-dimensional scalar array as a doctrine dql query result?](https://stackoverflow.com/questions/11657835/how-to-get-a-one-dimensional-scalar-array-as-a-doctrine-dql-query-result) – A.L Sep 14 '21 at 00:43

1 Answers1

1

Currently solved this by editing the getInvalidPrice getter:

public function getInvalidPrice(): ?int
{
    return $this->invalidPrice ? $this->invalidPrice->getHasInvalidPrice(): null;
}

I return the hasInvalidPrice property instead of the whole invalidPrice object.

The50
  • 1,096
  • 2
  • 23
  • 47