【问题标题】:Symfony Easy Admin Bundle: Preload nested form data - Fails Entity of type ...must be managedSymfony Easy Admin Bundle:预加载嵌套表单数据 - 失败类型的实体......必须管理
【发布时间】:2022-01-06 07:43:25
【问题描述】:

我正在尝试为这三个实例创建一个嵌套表单,其中库存有默认数据,嵌套表单InventoryProduct 默认在表单中包含数据库中的所有产品。

  • Inventory(有一个或多个InventarioProduct)-IdStartDateEndDate
  • 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


    【解决方案1】:

    错误告诉您,您正在使用类型为 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);
            }
        }
    

    Or add cascading persist on your entity.

    【讨论】:

      猜你喜欢
      • 2019-05-17
      • 2016-03-21
      • 2013-03-25
      • 2019-05-19
      • 2018-01-30
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      相关资源
      最近更新 更多