我正在使用Sonata开发Symfony项目。
语境:
我有不同的实体:
ID
,categories (relation)
,characteristicValues (relation)
)ID
,characteristics (relation)
)ID
,id_category (relation)
,label
)。ID
,id_product (relation)
,id_characteristic (relation)
,value
)关系:
--OneToMany-->
CharacteristicsValue-->OneToMany-->
特点-->OneToMany-->
CharacteristicsValue--ManyToMany-->
类别问题:
我需要在ProductAdmin中获取产品类别的所有characteristics
(以及它们设置时的values
),并显示每个产品的输入(如Characteristics1:value1)。
我做了什么:
我试图在CharacteristicValueRepository
中调用函数ProductAdmin
,但是没有实例化存储库。
ProductAdmin的代码非常基本:
final class ProductAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Product information', ['class' => 'col-md-6'])
->add('name', TextType::class, [
'label' => 'Name of the product'
])
->add('categories', EntityType::class, [
'class' => Category::class,
'choice_label' => 'name',
'multiple' => true,
'label' => 'Categories of the product'
])
->end();
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('name');
$datagridMapper->add('categories');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('id');
$listMapper->addIdentifier('name');
$listMapper->addIdentifier('categories');
}
}
笔记:
我正在使用一切的最新版本(Symfony,Sonata,...)
如果有人知道如何帮助我,我将非常感激!
您需要配置自定义表单类型,例如ProductCharacteristicsType。使用表单事件侦听器时,您可以获取所有特征并形成适当的集合。你在这里有EAV(实体属性值)模型。它可能会导致Symfony混淆,但它是可管理的。在SonataAdmin上,您必须使用您的自定义类型。