Иззет
Новичок
Здравствуйте,
Использую в проекте библиотеку https://github.com/KnpLabs/DoctrineBehaviors.
Возникла проблема при настройке фильтров в Sonata Admin. Привожу фрагменты.
Структура сущности:
Структура сущности переводов:
Cтруктура класса админа:
В итоге в табличном виде вместо названии категории отображается строка вида
что скорее всего происходит вследствие того, что метод просто возвращает объект с полями "name" на всех языках. В методе configureListFields() поле name выводится через метод proxyCurrentLocaleTranslation() (посредством волшебного метода __call()), где текущий язык берется из конфигурации. Как в данном случае поступить с фильтрами? Может кто уже сталкивался с такой проблемой. Спасибо заранее всем отозвавшимся.
Использую в проекте библиотеку https://github.com/KnpLabs/DoctrineBehaviors.
Возникла проблема при настройке фильтров в Sonata Admin. Привожу фрагменты.
Структура сущности:
PHP:
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* @ORM\Entity(repositoryClass="SiteBundle\Entity\Repository\CategoryRepository")
*/
class Category
{
use ORMBehaviors\Blameable\Blameable,
ORMBehaviors\Sortable\Sortable,
ORMBehaviors\Timestampable\Timestampable,
ORMBehaviors\Translatable\Translatable
;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
*/
private $children;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
public function __construct()
{
$this->children = new ArrayCollection();
}
public function __call($method, $arguments)
{
if (!method_exists(self::getTranslationEntityClass(), $method)) {
$method = 'get' . ucfirst($method);
}
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
public function setParent(Category $parent = null)
{
$this->parent = $parent;
return $this;
}
public function getParent()
{
return $this->parent;
}
/**
* Add children
*
* @param Category $children
* @return object
*/
public function addReply(Category $children)
{
$this->children[] = $children;
return $this;
}
/**
* Remove children
*
* @param Category $children
*/
public function removeReply(Category $children)
{
$this->children->removeElement($children);
}
/**
* Get children
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
}
PHP:
namespace SiteBundle\Entity;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class CategoryTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* @ORM\Column(type="string", length=255)
*/
protected $name;
/**
* @ORM\Column(type="string", length=255)
*/
protected $description;
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string
* @return null
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string
* @return null
*/
public function setDescription($description)
{
$this->description = $description;
}
}
PHP:
namespace SiteBundle\Admin;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class CategoryAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('translations', TranslationsType::class, [
'by_reference' => false,
])
->add('parent', 'sonata_type_model_autocomplete', [
'property' => 'translations.name',
'required' => false
])
;
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('translations.name', null, [
'label' => 'Name',
]);
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name', null, [
'label' => 'Name'
])
->add('parent')
->add('sort');
}
}
Код:
SiteBundle\Entity\Category:000000005f0b1...