我应该如何通过doctrine存储库查询中的鉴别器列进行排序?
我有一个非常直接的设置,我有不同类型的支付细节,它可以是信用卡(CC)或借记订单(DO).
所以我已经实现了单表继承映射策略来实现这一点,但是当我尝试通过鉴别器列进行排序时,问题就出现了,因为鉴别器列不存在于基类中.
存储库功能:
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();
}
Run Code Online (Sandbox Code Playgroud)
Base PaymentDetail实体:
/**
* @ORM\Entity(repositoryClass="AccountingBundle\Repository\PaymentDetailRepository")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\Table(name="PaymentDetails")
* @ORM\DiscriminatorColumn(name="PaymentMethodType", type="string")
* …Run Code Online (Sandbox Code Playgroud) 当在下面运行DQL时,从doctrine 2中选择仅识别器列时,我需要一些帮助
SELECT p.type FROM AppBundle\Entity\Product p
Run Code Online (Sandbox Code Playgroud)
type 是实体中的鉴别列 AppBundle\Entity\Product
@ORM\DiscriminatorColumn(name="type", type="smallint")`
@ORM\DiscriminatorMap({
"0" = "AppBundle\Entity\Product",
"1" = "AppBundle\Entity\Product\SingleIssue",
"2" = "AppBundle\Entity\Product\CountBasedIssue",
"3" = "AppBundle\Entity\Product\TimeBasedIssue"
})
Run Code Online (Sandbox Code Playgroud)
我知道这type不是实体中的不动产,但无论如何我还能这样做吗?
提前致谢!
阅读Doctrine代码2天后,我决定覆盖SqlWalker并通过下面的片段创建新的Hydrator
<?php
namespace ...;
use Doctrine\ORM\Query\SqlWalker;
class CustomSqlWalker extends SqlWalker
{
const FORCE_GET_DISCRIMINATOR_COLUMN = 'forceGetDiscriminatorColumn';
const DISCRIMINATOR_CLASS_MAP = 'discriminatorClassMap';
/**
* {@inheritdoc}
*/
public function walkSelectClause($selectClause)
{
$sql = parent::walkSelectClause($selectClause);
$forceGetDiscriminatorColumn = $this->getQuery()->getHint(self::FORCE_GET_DISCRIMINATOR_COLUMN);
if (empty($forceGetDiscriminatorColumn)) {
return $sql;
}
foreach ($this->getQueryComponents() as $key => $queryComponent) {
if (!in_array($key, …Run Code Online (Sandbox Code Playgroud)