Symfony Easy Admin Bundle:预加载嵌套表单数据 - 类型实体失败......必须管理
Symfony Easy Admin Bundle: Preload nested form data - Fails Entity of type ...must be managed
我正在尝试为这三个实例创建一个嵌套表单,其中库存有默认数据,嵌套表单InventoryProduct
在表单中默认包含数据库中的所有产品。
Inventory
(有一个或多个InventarioProduct
)- Id
, StartDate
, EndDate
InventoryProduct
- Id
, Product
, Units
, RejectedUnits
, QuarantineUnits
Product
- Id
、Name
、Inci
、产品的一些其他数据
所以我们将 InventoryCrudCrontroller
添加到 createEntityMethod
:
public function createEntity(string $entityFqcn)
{
$inventory= new Inventory();
$inventory->setStartDate(new DateTime('now'));
$inventory->setEndDate(null);
$productRepository= $this->entityManager->getRepository(MateriaPrima::class);
$products= $productRepository->findAll();
foreach ($products as $product) {
$inventoryProduct= new InventoryProduct();
$inventoryProduct->setProduct($product);
$inventoryProduct->setUnits(0);
$inventoryProduct->setUnitsRejected(0);
$inventoryProduct->setUnitsQuarantine(0);
$inventoryProduct->setInventory($inventory);
$inventory->addInventarioProduct($inventoryProduct);
}
以及 InventoryCrudCrontroller
上的 configureFields
方法:
public function configureFields(string $pageName): iterable
{
if (Crud::PAGE_EDIT === $pageName || Crud::PAGE_NEW == $pageName) {
return [
DateTimeField::new('startDate')
->setColumns(6)
->setValue(new DateTime()),
DateTimeField::new('endDate')
->setColumns(6),
CollectionField::new('products', 'Products:')
->onlyOnForms()
->allowAdd()
->allowDelete()
->setEntryIsComplex(false)
->setEntryType(InventoryProductType::class)
->renderExpanded(true)
->setFormTypeOptions(
[
'by_reference' => false,
]
)
->setColumns(12),
然后我们为海关表格添加 class InventoryProductType
:
class InventoryProducts extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add(
'product',
EntityType::class,
['class' => Product::class, 'label' => '-']
)
->add('units')
->add('unitsRejected')
->add('unitsQuarantine')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => InventoryProduct::class,
]);
}
}
当我们尝试添加另一个注册表时,我们得到:
Entity of type "App\Entity\Inventory" passed to the choice field must
be managed. Maybe you forget to persist it in the entity manager?
我做错了什么?
感谢您的帮助!!
该错误告诉您您正在使用类型为 App\Entity\Inventory
的实体,该实体不受实体管理器管理。
如果您查看您的代码,您会在 createEntity
方法中创建一个新实体,但从未持久化它。
您可以将其坚持在您的方法中,例如:
public function createEntity(string $entityFqcn)
{
$inventory= new Inventory();
$inventory->setStartDate(new DateTime('now'));
$inventory->setEndDate(null);
$this->entityManager->persist($inventory);
$productRepository= $this->entityManager->getRepository(MateriaPrima::class);
$products= $productRepository->findAll();
foreach ($products as $product) {
$inventoryProduct= new InventoryProduct();
$inventoryProduct->setProduct($product);
$inventoryProduct->setUnits(0);
$inventoryProduct->setUnitsRejected(0);
$inventoryProduct->setUnitsQuarantine(0);
$inventoryProduct->setInventory($inventory);
$this->entityManager->persist($inventoryProduct);
$inventory->addInventarioProduct($inventoryProduct);
}
}
我正在尝试为这三个实例创建一个嵌套表单,其中库存有默认数据,嵌套表单InventoryProduct
在表单中默认包含数据库中的所有产品。
Inventory
(有一个或多个InventarioProduct
)-Id
,StartDate
,EndDate
InventoryProduct
-Id
,Product
,Units
,RejectedUnits
,QuarantineUnits
Product
-Id
、Name
、Inci
、产品的一些其他数据
所以我们将 InventoryCrudCrontroller
添加到 createEntityMethod
:
public function createEntity(string $entityFqcn)
{
$inventory= new Inventory();
$inventory->setStartDate(new DateTime('now'));
$inventory->setEndDate(null);
$productRepository= $this->entityManager->getRepository(MateriaPrima::class);
$products= $productRepository->findAll();
foreach ($products as $product) {
$inventoryProduct= new InventoryProduct();
$inventoryProduct->setProduct($product);
$inventoryProduct->setUnits(0);
$inventoryProduct->setUnitsRejected(0);
$inventoryProduct->setUnitsQuarantine(0);
$inventoryProduct->setInventory($inventory);
$inventory->addInventarioProduct($inventoryProduct);
}
以及 InventoryCrudCrontroller
上的 configureFields
方法:
public function configureFields(string $pageName): iterable
{
if (Crud::PAGE_EDIT === $pageName || Crud::PAGE_NEW == $pageName) {
return [
DateTimeField::new('startDate')
->setColumns(6)
->setValue(new DateTime()),
DateTimeField::new('endDate')
->setColumns(6),
CollectionField::new('products', 'Products:')
->onlyOnForms()
->allowAdd()
->allowDelete()
->setEntryIsComplex(false)
->setEntryType(InventoryProductType::class)
->renderExpanded(true)
->setFormTypeOptions(
[
'by_reference' => false,
]
)
->setColumns(12),
然后我们为海关表格添加 class InventoryProductType
:
class InventoryProducts extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add(
'product',
EntityType::class,
['class' => Product::class, 'label' => '-']
)
->add('units')
->add('unitsRejected')
->add('unitsQuarantine')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => InventoryProduct::class,
]);
}
}
当我们尝试添加另一个注册表时,我们得到:
Entity of type "App\Entity\Inventory" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?
我做错了什么?
感谢您的帮助!!
该错误告诉您您正在使用类型为 App\Entity\Inventory
的实体,该实体不受实体管理器管理。
如果您查看您的代码,您会在 createEntity
方法中创建一个新实体,但从未持久化它。
您可以将其坚持在您的方法中,例如:
public function createEntity(string $entityFqcn)
{
$inventory= new Inventory();
$inventory->setStartDate(new DateTime('now'));
$inventory->setEndDate(null);
$this->entityManager->persist($inventory);
$productRepository= $this->entityManager->getRepository(MateriaPrima::class);
$products= $productRepository->findAll();
foreach ($products as $product) {
$inventoryProduct= new InventoryProduct();
$inventoryProduct->setProduct($product);
$inventoryProduct->setUnits(0);
$inventoryProduct->setUnitsRejected(0);
$inventoryProduct->setUnitsQuarantine(0);
$inventoryProduct->setInventory($inventory);
$this->entityManager->persist($inventoryProduct);
$inventory->addInventarioProduct($inventoryProduct);
}
}