1

I have in my entity:

/**
 * @ORM\ManyToMany(targetEntity="Sancho\UserBundle\Entity\followers", mappedBy="followers")
 */
private $followers;

In Sonata i want to get the number of $followers[]. I create in my entity:

public function getFollow()
{
      return count($this->followers);
}

In UserAdmin i have:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('id')
            ->add('follow', null, array('sortable'=>true));
}

I have the number of $follow[] but i can't sort the field. When i sort i have the error:

PHP Catchable fatal error:  Argument 1 passed to Sonata\\DoctrineORMAdminBundle\\Datagrid\\ProxyQuery::entityJoin() must be of the type array, null given, called in /home/sancho/work/Web/vendor/sonata-project/doctrine-orm-admin-bundle/Datagrid/ProxyQuery.php on line 140 and defined in /home/sancho/work/Web/vendor/sonata-project/doctrine-orm-admin-bundle/Datagrid/ProxyQuery.php on line 245
Sancho
  • 1,288
  • 2
  • 25
  • 50
  • you can implement this solution: [http://stackoverflow.com/questions/36153381/sort-list-view-in-sonata-admin-by-related-entity-fields](http://stackoverflow.com/questions/36153381/sort-list-view-in-sonata-admin-by-related-entity-fields) – Andres Apr 10 '17 at 01:58

2 Answers2

1

Your looking in the wrong direction... Sortable in sonata is used to define a field that will hold a position number so you can indeed order the things but based on indexes it writes for itself, so it requires a field in the database thats why you get that error. What you need to do is override the createQuery method in the admin class with ordering functionality. Take a look here how to do that.

Geert Wille
  • 1,656
  • 13
  • 18
  • 1
    Do you have an example with a OneToMany link ? I have 2 entities (User and Follow). For the moment i use getFollow() {return count($this->followers;}. How can i sort my field by the number of "follow" with queryBuilder ? – Sancho Feb 25 '14 at 09:16
  • he suggests doing a group by. but that does not work. still looking for a solution – Romain Bigz Nov 22 '16 at 10:50
  • I am getting crazy with this issue, did anyone find a solution ? – Florent Destremau Jan 02 '17 at 16:58
0

I am not sure if this applies to your situation. But I believe the problem may be how you access the field.

->add('follow', null, array('sortable'=>true));

You should use

->add('followers.follow');

I had a relation between a 'store' and 'product' and I used ManyToOnerelations and from the relation table I had ->addIdentifier('store') which apparently uses __toString method and therefore can't be sorted. However when I associate ->addIdentifier('store.name') it is the name field which can be sorted.

Evren Yurtesen
  • 2,267
  • 1
  • 22
  • 40