我应该如何通过学说知识库查询中的鉴别器列进行排序?
我有一个很直接的设置, 我有不同类型的付款细节, 它可以是信用卡(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();
}
Base PaymentDetail实体:
/**
* @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... */
}
借记订单PaymentDetail实体:
/**
* 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... */
}
信用卡PaymentDetail实体:
/**
* 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... */
}
当我尝试时,我收到此错误,
Error: Class AccountingBundle\Entity\PaymentDetail has no field or association named method_type")
答案 0 :(得分:3)
尽管TYPE尚未实现到学说的核心,但仍然可以将其作为自定义用户函数实现。
有人已经做了一个了不起的工作,flow.org/try给了我们。为了防止将来删除该资源,这里是php7 +的一个稍微调整过的版本:
<?php
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\SqlWalker;
class TypeFunction extends FunctionNode
{
/**
* @var string
*/
public $dqlAlias;
public function getSql(SqlWalker $sqlWalker): string
{
/** @var ClassMetadataInfo $class */
$class = $sqlWalker->getQueryComponent($this->dqlAlias)['metadata'];
$tableAlias = $sqlWalker->getSQLTableAlias($class->getTableName(), $this->dqlAlias);
if (!isset($class->discriminatorColumn['name'])) {
$message = 'TYPE() only supports entities with a discriminator column.';
throw QueryException::semanticalError($message);
}
return $tableAlias . '.' . $class->discriminatorColumn['name'];
}
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->dqlAlias = $parser->IdentificationVariable();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
现在您可以通过执行以下操作,通过鉴别器列进行排序:
SELECT e, TYPE(e) AS HIDDEN my_type FROM Entity e ORDER BY my_type DESC;
答案 1 :(得分:1)
似乎最简单的解决方案(到目前为止)是向基类添加另一个字段并复制鉴别器列值。
上述TYPE(q)
仅适用于WHERE子句。
答案 2 :(得分:0)
您可以尝试使用TYPE()
或INSTANCE OF
吗?
相关:https://groups.google.com/forum/#!topic/doctrine-user/JtCbwuN-37o
但是,本主题并未说明是否实施。在写这篇文章的时候有人说按顺序不行。
$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 TYPE(pd)';