EasyAdminBundle: setDefaultSort not working when sorting on association field

Describe the bug I’m trying to configure CRUD to sort entities by a field contained within an association

 public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->setEntityLabelInSingular('User')
            ->setEntityLabelInPlural('Users')
            ->setSearchFields(['email'])
            ->setPaginatorPageSize(100)
            ->setDefaultSort(['account.last_name' => 'ASC'])
            ->setEntityPermission('ROLE_ADMIN');
    }

Each User entity has a Account associated.

The error raided is [Semantical Error] line 0, col 138 near 'last_name AS': Error: Class App\Entity\User has no field or association named account.last_name and the query exception SELECT entity FROM App\Entity\User entity LEFT JOIN entity.account account WHERE entity.roles LIKE '%ROLE_ADMIN%' ORDER BY entity.account.last_name ASC

User.php

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User implements UserInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Account", mappedBy="user", cascade={"persist", "remove"})
     * @Assert\Valid()
     */
    private $account;

    public function getAccount(): ?Account
    {
        return $this->account;
    }

    public function setAccount(?Account $account): self
    {
        $this->account = $account;

        // set (or unset) the owning side of the relation if necessary
        $newUser = null === $account ? null : $this;
        if ($account->getUser() !== $newUser) {
            $account->setUser($newUser);
        }

        return $this;
    }
}

Account.php

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass="App\Repository\AccountRepository")
 * @Vich\Uploadable
 */
class Account implements \Serializable
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\User", cascade={"persist", "remove"}, inversedBy="account")
     */
    private $user;


    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;

        return $this;
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

This incident comes from this change https://github.com/EasyCorp/EasyAdminBundle/commit/80e2cc78c301c999f1838a91eaa7d58106e887d2

For people who can’t wait for a fix i recommend downgrade to 3.1.5.

Thank you @jgwiazdowski and @krewetka.

I prepared a little fix that will work with both types of sorting. WDYT? https://github.com/EasyCorp/EasyAdminBundle/pull/3886

what looks like an issue - in my opinion - is this, let’s look at my example, in my case I have Transaction and Trade entities, one Trade may have many Transactions and one Transaction may have only on Trade, so it’s just typical ManyToOne

here is query which fails SELECT entity FROM App\Entity\Transaction entity LEFT JOIN entity.trade trade ORDER BY entity.trade.status DESC

shouldn’t that be actually be this? SELECT entity FROM App\Entity\Transaction entity LEFT JOIN entity.trade trade ORDER BY trade.status DESC ? it’s basically an attempt to try to sort by status from trade entity

Issue above is not only related with default sorting, if you actually have a field like account.last_name in your fields list and then try to sort by that column (by clicking on it) you would get a same error, I tried to do a join in createIndexQueryBuilder but then I end up with an error like Error: 'trade' is already defined. (if your case it would probably say “account” is already defined)

There is similar problem when you try to add this value to ->setSearchFields - it does not work as well.

Btw, problem in SQL above is ORDER BY entity.account.last_name -> should be just ORDER BY account.last_name

@malteschlueter seems to be working fine, many thanks