我正在尝试为这三个实例创建一个嵌套表单,其中库存具有默认数据,并且嵌套表单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);
}
在configureFields
方法上InventoryCrudCrontroller
:
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),
我们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,
]);
}
}
当我们尝试添加另一个注册表时,我们得到:
必须管理传递给选择字段的“App\Entity\Inventory”类型的实体。也许您忘记将其保留在实体管理器中?
我究竟做错了什么?
谢谢你的帮助!!