Doctrine2 - 如何通过鉴别器列进行订购?

Ody*_*3us 15 doctrine dql symfony doctrine-orm

我应该如何通过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")
 * @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... */
}
Run Code Online (Sandbox Code Playgroud)

借记订单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... */
}
Run Code Online (Sandbox Code Playgroud)

信用卡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... */
}
Run Code Online (Sandbox Code Playgroud)

当我尝试这个时,我得到了这个错误,

Error: Class AccountingBundle\Entity\PaymentDetail has no field or association named method_type") 
Run Code Online (Sandbox Code Playgroud)

Flo*_*ian 0

你可以尝试使用TYPE()orINSTANCE OF吗?

相关:https ://groups.google.com/forum/#!topic/doctrine-user/JtCbwuN-37o

但是,这个主题并没有说明它是否已实现。当时有人说 order by 不起作用。

$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)';
Run Code Online (Sandbox Code Playgroud)