Discussion:
[doctrine-user] How to use polymorphism with Doctrine?
Dennis Matveyev
2018-02-15 14:48:21 UTC
Permalink
Consider this code:
public function getRepository()
{
return $this->getDoctrine()->getRepository(ProductPricing::class);
}

public function getPriceObject(int $productId)
{
/**
*
* @var $priceObject ProductPricing
*/
return $this->getRepository()->findOneBy(array(
'productId' => $productId
));
}

The above works great if all we have is a single entity. But how could I
extend this when I need a polymorphic entity?
i.e I have AProductPricing::class, BProductPricing::class, and so on?

I have created the following code, but I am not certain how to proceed:
class PricingEntityFactory
{

function __invoke(string $product): ProductPricing
{
switch (strtoupper($product))
{
case "A":
return new AProductPricing();

case "B":
return new BProductPricing();

case "D":
return new CProductPricing();
}
}
}

My goal is, when I have a $product that has one of {"A", "B", "C"}, to use
Doctrine to pull the record using the entity one of AProductPricing,
BProductPricing, CProductPricing.
How?
--
You received this message because you are subscribed to the Google Groups "doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to doctrine-user+***@googlegroups.com.
To post to this group, send email to doctrine-***@googlegroups.com.
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.
Marco Pivetta
2018-02-15 14:49:18 UTC
Permalink
See https://github.com/doctrine/doctrine2/issues/6072#issuecomment-363038318

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/
Post by Dennis Matveyev
public function getRepository()
{
return $this->getDoctrine()->getRepository(ProductPricing::class);
}
public function getPriceObject(int $productId)
{
/**
*
*/
return $this->getRepository()->findOneBy(array(
'productId' => $productId
));
}
The above works great if all we have is a single entity. But how could I
extend this when I need a polymorphic entity?
i.e I have AProductPricing::class, BProductPricing::class, and so on?
class PricingEntityFactory
{
function __invoke(string $product): ProductPricing
{
switch (strtoupper($product))
{
return new AProductPricing();
return new BProductPricing();
return new CProductPricing();
}
}
}
My goal is, when I have a $product that has one of {"A", "B", "C"}, to use
Doctrine to pull the record using the entity one of AProductPricing,
BProductPricing, CProductPricing.
How?
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to doctrine-user+***@googlegroups.com.
To post to this group, send email to doctrine-***@googlegroups.com.
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.
Loading...