Sonata Admin-在关系字段上排序

时间:2018-11-09 10:27:40

标签: php symfony sonata-admin sonata columnsorting

在我的后台(由 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 :(得分:0)

您不能使用“名称”代替翻译[en] .name吗?