【发布时间】:2022-01-06 07:43:25
【问题描述】:
我正在尝试为这三个实例创建一个嵌套表单,其中库存有默认数据,嵌套表单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),
我们为海关表格添加类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”类型的实体必须 被管理。也许您忘记将其持久化在实体管理器中?
我做错了什么?
感谢您的帮助!!
【问题讨论】:
标签: php symfony easyadmin easyadmin3