How should I go about ordering by a discriminator column in a doctrine repository query?
I have a pretty straight forward setup, I have different types of payment details, it can either be Credit Card (CC) or Debit Order (DO).
So I've implemented a single table inheritance mapping strategy to achieve this, but the problem now comes in when I try to order by the discriminator column, since the discriminator column isn't present in the base class.
The repository function:
public function getPaymentDetails (ClientContactInterface $clientContact)
{
$dql = 'SELECT pd
from
AccountingBundle:PaymentDetail pd
JOIN ClientProductBundle:ClientProduct cp
WITH cp.payment_detail_id = pd.id
WHERE
cp.payment_detail_id = pd.id
and cp.client_contact_id = :client_contact_id
GROUP BY pd.id
ORDER BY pd.method_type'; // Since pd.method_type is the discriminator column, I cannot order by it. And I need to be able to.
$em = $this->getEntityManager();
$query = $em->createQuery($dql)->setParameter('client_contact_id', $clientContact->getId());
return $query->getResult();
}
Base PaymentDetail entity:
/**
* @ORM\Entity(repositoryClass="AccountingBundle\Repository\PaymentDetailRepository")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\Table(name="PaymentDetails")
* @ORM\DiscriminatorColumn(name="PaymentMethodType", type="string")
* @ORM\DiscriminatorMap({ "DO" = "DOPaymentDetail", "CC" = "CCPaymentDetail"})
*/
class PaymentDetail implements PaymentDetailInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/* etc... */
}
Debit Order PaymentDetail entity:
/**
* AccountingBundle\Entity\DOPaymentDetail
*
* @ORM\Table(name="PaymentDetails")
* @ORM\Entity
*/
class DOPaymentDetail extends PaymentDetail implements DOPaymentDetailInterface
{
/**
* @var string $account_holder
*
* @ORM\Column(name="DOAccountHolder", type="string", length=255)
*/
protected $account_holder;
/* etc... */
}
Credit Card PaymentDetail entity:
/**
* AccountingBundle\Entity\CCPaymentDetail
*
* @ORM\Table(name="PaymentDetails")
* @ORM\Entity
*/
class CCPaymentDetail extends PaymentDetail implements CCPaymentDetailInterface
{
/**
*
* @var string $card_holder
*
* @ORM\Column(name="CCCardHolder", type="string", length=255)
*/
protected $card_holder;
/* etc... */
}
When I try that, I get this error,
Error: Class AccountingBundle\Entity\PaymentDetail has no field or association named method_type")