Sonata Admin - сортировка по полю отношений - PullRequest
0 голосов
/ 09 ноября 2018

В моем бэк-офисе (сделано с Sonata Admin ) у меня есть обзор клиентов.

Моя Модель клиента выглядит так:

<?php

namespace MyProject\Domain\Model\Customer;

use Doctrine\Common\Collections\ArrayCollection;
use MyProject\Domain\Model\AddressTrait;
use MyProject\Domain\Model\HasAddressInterface;
use MyProject\Domain\Model\Project\InternalProjectInterface;

/**
 * Class: Customer
 *
 * The customer name with an associated address
 *
 * @see CustomerInterface
 * @see HasAddressInterface
 */
class Customer implements CustomerInterface, HasAddressInterface
{
    use AddressTrait;

    /** @var int */
    protected $id;

    /** @var \Doctrine\Common\Collections\Collection */
    protected $contacts;

    /** @var \Doctrine\Common\Collections\Collection */
    protected $projects;

    /**
     * Customer constructor.
     */
    public function __construct()
    {
        $this->contacts = new ArrayCollection();
    }

    /**
     * @inheritDoc
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @inheritDoc
     */
    public function getContacts()
    {
        return $this->contacts;
    }

    /**
     * @inheritDoc
     */
    public function addContact(ContactInterface $contact)
    {
        /** @var $contact \MyProject\Domain\Model\HasCustomerInterface */
        $contact->setCustomer($this);

        return $this->contacts->add($contact);
    }

    /**
     * @inheritDoc
     */
    public function removeContact(ContactInterface $contact)
    {
        /** @var $contact \MyProject\Domain\Model\HasCustomerInterface */
        $contact->setCustomer(null);

        return $this->contacts->removeElement($contact);
    }

    /**
     * @inheritDoc
     */
    public function getProjects()
    {
        return $this->projects;
    }

    /**
     * @inheritDoc
     */
    public function addProject(InternalProjectInterface $project)
    {
        /** @var $project \MyProject\Domain\Model\HasCustomerInterface */
        $project->setCustomer($this);

        return $this->projects->add($project);
    }

    /**
     * @inheritDoc
     */
    public function removeProject(InternalProjectInterface $project)
    {
        /** @var $project \MyProject\Domain\Model\HasCustomerInterface */
        $project->setCustomer(null);

        return $this->projects->removeElement($project);
    }

    /**
     * @inheritDoc
     */
    public function __toString()
    {
        return empty($this->getName()) ? "New customer" : $this->getName();
    }
}

В моем CustomerAdmin классе у меня есть:

public function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('translations[en].name', null, [
            'label' => 'Name'
        ])
        ->add('address.street', null, [
            'label' => 'Street and number'
        ])
        ->add('address.postal_code', null, [
            'label' => 'Postal code',
        ])
        ->add('address.translations[en].city', null, [
            'label' => 'City',
        ])
        ->add('_action', null, [
            'actions' => [
                'edit' => [],
                'delete' => [],
            ]
        ])
    ;
}

Проблема в том, что я не могу отсортировать по имени поля. Это выглядит так:

enter image description here

У меня также есть модель CustomerTranslation , которая выглядит следующим образом:

<?php

namespace MyProject\Domain\Model\Customer;

use MyProject\Domain\Model\NameTrait;

/**
 * Class: CustomerTranslation
 *
 * Translated information of a customer
 *
 * @see CustomerTranslationInterface
 */
class CustomerTranslation implements CustomerTranslationInterface
{
    use NameTrait;

    /** @var int */
    protected $id;

    /**
     * @inheritDoc
     */
    public function getId()
    {
        return $this->id;
    }
}

Но как я могу убедиться, что можно также сортировать по имени в обзоре клиентов?

1 Ответ

0 голосов
/ 09 ноября 2018

Не могли бы вы использовать «имя» вместо перевода [en] .name?

...