Sonata admin - “order by” field in related table

后端 未结 2 638
北海茫月
北海茫月 2021-02-08 19:04

I have a Product admin class. The Product entity has a many-to-one relationship with a Category entity, i.e. a product is associated with a category.

In the admin \"list

2条回答
  •  别那么骄傲
    2021-02-08 19:23

    It seems a workaround, but it works. You have to add a join overriding createQuery() method, than assign a default sortBy overriding $datagridValues:

     1,
            '_sort_order' => 'ASC', // sort direction
            '_sort_by' => 'c.name' // field name
        );
    
        /**
         * @return \Sonata\AdminBundle\Datagrid\ProxyQueryInterface
         */
        public function createQuery($context = 'list')
        {
            $query = parent::createQuery($context);
    
            return new ProxyQuery($query
                ->join(sprintf('%s.category', $query->getRootAlias()), 'c'));
        }
    }
    

提交回复
热议问题