Symfony2 Sonata Project sonata_type_model with OneToMany entity sortby

Symfony2 Sonata Project sonata_type_model with OneToMany entity sortby

我与 Symfony2 和奏鸣曲管理员一起工作。我有一个拥有子类别的实体(新闻)。每个子类别拥有一个类别,每个类别拥有一个 Affaire。 在新闻的添加页面中,我有一个子类别列表,可以选择我的子类别 link 到我的新闻。我的 select 中的每一项的格式如下:

<li> subcategory (category'affaire) > categoryName </li>.

我想按 affaire (ASC) 对字段进行排序。

这是我的表单域定义:

protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('subCategory', 'sonata_type_model', array("label" => "Catégorie/Sous Catégorie", "btn_add" => false));
}

一个新闻拥有一个子类别

一个子类别拥有一个类别

一个类别拥有一个事件。

我尝试添加如下内容:

    ->add('subCategory', 'sonata_type_model', 
            array("label" => "Sub Category",
                  "btn_add" => false
            ), 
            array(
                'sortable'      => 'ordering',
                'label'         => 'subcategory.category.affaire.code',
            ))

但没有任何变化。有什么想法吗?

类别实体:

class NewsCategory
{
    /**
     * @var \My\Custom\Foo\Entity\Affaire
     *
     * @ORM\ManyToOne(targetEntity="\My\Custom\Foo\Entity\Affaire")
     * @ORM\JoinColumn(name="affaire_code", referencedColumnName="code")
     */
    private $affaire;

-- 子类实体:

class NewsSubCategory
{
    /**
     * @var \My\Custom\Foo\Entity\NewsCategory
     *
     * @ORM\ManyToOne(targetEntity="\My\Custom\Foo\Entity\NewsCategory")
     * @ORM\JoinColumn(name="category_ref", referencedColumnName="id")
     * 
     */
    private $category;

-- 新闻实体:

class News
{
    /**
     * @var \My\Custom\Foo\Entity\NewsSubCategory
     *
     * @ORM\ManyToOne(targetEntity="\My\Custom\Foo\Entity\NewsSubCategory")
     * @ORM\JoinColumn(name="sub_category", referencedColumnName="id")
     */
    private $subCategory;

[编辑]:

我试过了

->add('subCategory', 'sonata_type_model',array("label" => "Catégorie/Sous Catégorie","btn_add" => false), array("sortable" => "ordering"))

我没有出错,但什么也没发生。我不明白我可以在哪里添加选项 (orderBy => 'Affaire') ,或者是否必须这样做...

[编辑2] :

我什至试过了:

->add('subCategory.category.affaire', null,
                    array("label" => "Catégorie/Sous Catégorie",
                            "btn_add" => false
                    ))

似乎'Sortable'必须在第三个参数中。

->add('subCategory', 'sonata_type_model', 
        array("label" => "Sub Category",
              "btn_add" => false
              'sortable'      => 'ordering',
        ))

之后,你有两个选择:尝试展示外遇

->add('subCategory.categorie.affaire', 'sonata_type_model', 
        array("label" => "Affaire",
              "btn_add" => false
              'sortable'      => 'ordering',
        ))

或者我们的实体可以实现 "Collections Sortable" 。尝试看看:https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/sortable.md

(抱歉,我的英文不流利)

我不知道如何使用 sonata_type_model 来做到这一点,但您可以将字段类型更改为 null 或实体(null 设置默认类型)并添加 query_builder调整所用查询的选项:

->add('subcategory', null, array(
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('sc')
            ->leftjoin('sc.category', 'c')
            ->orderBy('c.affaire', 'ASC');
    }
))

如果您选择实体而不是 null,则还必须添加 class :

->add('subcategory', 'entity', array(
    'class' => 'MyCustomFooBundle:Subcategory',
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('sc')
            ->leftjoin('sc.category', 'c')
            ->orderBy('c.affaire', 'ASC');
    }
))