简体   繁体   中英

Symfony2 Sonata Project sonata_type_model with OneToMany entity sortby

I work with Symfony2 and sonata admin. I have an entity (News) which own a subcategory. Each subcategory own one category, and each category own one Affaire. In the add page for a news, I have a subcategory list, to choose my subcategory to link to my news. Each item of my select is formated like this :

在此处输入图片说明

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

I would like to sort the fields by the affaire (ASC).

Here is my formfield definition :

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

One News own one Subcategory

One Subcategory own one Category

One category own one Affaire.

I tried to add something like :

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

But nothing changes. Any Ideas ?

Category entity :

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;

-- Subcategory entity :

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;

-- News entity :

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;

[EDIT] :

I tried

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

And I doesn't make an error but nothing happend. I don't understand where i could add the option (orderBy => 'Affaire') , or if it has to be done that way ...

[EDIT2] :

I even tried :

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

I don't know how you can do that with a sonata_type_model but you can change the type of your field to null or entity (null set the default type) and add a query_builder option to adapt the query used :

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

If instead of null you choose entity, you have to add the class also :

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

It seems that 'Sortable' had to be in the third parameter.

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

And after that, you have two options : Trying to show the Affaire

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

Or our Entity can implement "Collections Sortable" . Try to have a look to : https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/sortable.md

(Sorry, I'm not fluent in english)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM