Sonata Admin - 按关系字段排序
Posted
技术标签:
【中文标题】Sonata Admin - 按关系字段排序【英文标题】:Sonata Admin - sort on relation field 【发布时间】:2019-04-12 22:03:19 【问题描述】:在我的后台(使用 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' => [],
]
])
;
问题是我无法按字段名称排序。它看起来像这样:
我还有一个模型 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】:你不能用“名字”代替翻译[en].name 吗?
【讨论】:
以上是关于Sonata Admin - 按关系字段排序的主要内容,如果未能解决你的问题,请参考以下文章