如何使用Doctrine通过不是DB列的属性进行排序? [英] How do I order by a property that isn't a DB column using Doctrine?

查看:130
本文介绍了如何使用Doctrine通过不是DB列的属性进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在定义关系时,相关模型上有一个属性(不是DB列),但是我想按它排序(在 @OrderBy 注释中)

When defining a relationship, there is a property on the related model (not a DB column), but I would like to sort by it (in the @OrderBy annotation).

我有一个使用单表继承。所涉及的属性基本上是每个子类中指定的订单属性,但不保存到数据库。

I have a base model that is extended using single table inheritance. The property in question is basically an order property that is specified in each child class, but is not saved to the DB.

(我不想向DB表添加一个订单列,因为排序完全取决于鉴别器映射到哪个子类。一个独特的约束,以便每个孩子的类可以在关系中使用不超过一次。)

(I don't want to add an order column to the DB table, since the ordering depends purely on which child class the discriminator is mapped to. There is already a unique constraint so that each child class can be used no more than once in the relationship.)

这是一个真正简化的代码...

Here's a really simplified version of my code...

/**
 * @ORM\Entity
 * @ORM\Table(name="base")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="class_name", type="string")
 *
 * @ORM\DiscriminatorMap({
 *   "Base" = "Models\Base",
 *   "ChildA" = "Models\ChildB",
 *   "ChildB" = "Models\ChildA"
 * })
 **/
class Base 
{
    /** @ORM\Column(type="string") **/
    protected $class_name;

    /**
     * @ORM\ManyToOne(targetEntity="Related", inversedBy="collection")
     **/
    protected $related;

    // this is just a plain ol' property (not in the DB)
    protected $order;

    public function getClassName()
    {
        return $this->class_name;
    }
}



儿童:



Children:

/**
 * @ORM\Entity
 * @ORM\Table(name="child_a")
 **/
class ChildA extends Base
{
    $order = 1;
}

/**
 * @ORM\Entity
 * @ORM\Table(name="child_b")
 **/
class ChildB extends Base
{
    $order = 2;
}



相关实体:



Related entity:

/**
 * @ORM\Entity
 * @ORM\Table(name="related")
 **/
class Related
{
    /**
     * @ORM\OneToMany(targetEntity="Base", mappedBy="related")
     * @ORM\OrderBy({"order"="ASC"})
     **/
    protected $collection;

    public function getCollection()
    {
        $em = App::make('Doctrine\ORM\EntityManagerInterface');

        // map each Base instance to the appropriate child class
        return $this->collection->map(function ($base) use ($em) {
            $class_name = $base->getClassName();
            return $em->find($class_name, $base->getId());
        });
    }
}

是否可以使用订单属性,以订购集合关系? (根据 class_name 使用开关类型的构造也是有效的,但是我没有找到任何方式要么这样做,而且更难维护。)

Is it possible to use the order property for ordering the collection relationship? (Ordering based on class_name using a switch-like construct would also be valid, but I haven't found any way to do that either, and it would be harder to maintain.)

提前感谢

推荐答案

从ORM开始的指令非常清楚,你正在引用一个与表字段有关系的属性。您不能对不存在的字段使用ORM伪指令。 教义注释:OrderBy

The directive beginning with ORM is very much telling Doctrine you're doing referencing a property that has a relationship with a table field. You can't use ORM directives on fields that don't exist. Doctrine annotations: OrderBy

您必须在函数中实现此功能,最好在模型本身(在您的getCollection()函数中),或者如果您正在使用框架就像Symfony把它放在这个实体的存储库类的一个函数中。您必须使用PHP排序功能才能执行此操作。因为该属性与表中的某个字段无关,因此SQL / DQL将无法正常工作。

You would have to implement this in a function, best in the model itself (within your getCollection() function), or if you're using a framework like Symfony place it in a function of the repository class for this entity. You'd have to use PHP sorting functions to do this. SQL/DQL won't work either because the property isn't related to a field in the table.

这篇关于如何使用Doctrine通过不是DB列的属性进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆