gpt4 book ai didi

symfony - Doctrine2 - 如何按鉴别器列排序?

转载 作者:行者123 更新时间:2023-12-03 20:56:52 25 4
gpt4 key购买 nike

我应该如何通过 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();
}

基本 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") 

最佳答案

尽管 TYPE 尚未实现到 Doctrine 的核心,但仍然可以将其实现为自定义用户功能。

有人已经完成了一项了不起的工作和 implemented对我们来说。以防万一该资源将来会被删除,这里是 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;

关于symfony - Doctrine2 - 如何按鉴别器列排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19405866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com