Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony CollectionType编辑表单中的多对多关系_Php_Doctrine Orm_Symfony 3.4 - Fatal编程技术网

Php Symfony CollectionType编辑表单中的多对多关系

Php Symfony CollectionType编辑表单中的多对多关系,php,doctrine-orm,symfony-3.4,Php,Doctrine Orm,Symfony 3.4,我有3个实体的积垢膳食,产品和产品质量。膳食和产品数量之间是多对多的关系。添加数据工作正常,所有数据都保存到实体中,但问题是何时需要编辑表单。然后我得到了一个错误: 表单的视图数据应该是类MealBundle\Entity\ProductsQuantity的实例,但却是类Doctrine\ORM\PersistentCollection的实例。通过将“data\u class”选项设置为null,或者添加一个视图转换器,将classDoctrine\ORM\PersistentCollectio

我有3个实体的积垢<代码>膳食,
产品
产品质量
膳食
产品数量
之间是多对多的关系。添加数据工作正常,所有数据都保存到实体中,但问题是何时需要编辑表单。然后我得到了一个错误:

表单的视图数据应该是类
MealBundle\Entity\ProductsQuantity
的实例,但却是类
Doctrine\ORM\PersistentCollection
的实例。通过将
“data\u class”
选项设置为
null
,或者添加一个视图转换器,将class
Doctrine\ORM\PersistentCollection
的实例转换为
MealBundle\Entity\ProductsQuantity
的实例,可以避免此错误

我尝试将
data\u class
选项设置为
null
,并在关系上设置
fetch=“EAGER”
,但没有解决问题

膳食:

产品数量:

膳食形式:

产品质量表:

如果我将
'data'=>['productsQuantity'=>$data]
更改为
'data'=>['productsQuantity'=>new productsQuantity()]
则没有错误,但
productsQuantity
表单的一部分为空。谁能告诉我怎么解决这个问题吗

/**
 * @Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 */
protected $id;

/**
 * @var string
 * @ORM\Column(name="name", type="string", length=255)
 */
protected $name = "";

/**
 * @var ProductsQuantity[]|Collection
 * @ORM\ManyToMany(targetEntity="ProductsQuantity", inversedBy="meal", cascade={"persist"}, fetch="EAGER")
 * @ORM\JoinTable(name="meal_products_quantity_relations",
 *     joinColumns={@ORM\JoinColumn(name="meal_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="products_quantity_id", referencedColumnName="id")}
 * )
 */
protected $productsQuantity;


/**
 * Meal constructor.
 */
public function __construct()
{
    $this->productsQuantity = new ArrayCollection();
}

/**
 * @return int
 */
public function getId(): int
{
    return $this->id;
}

/**
 * @param int $id
 * @return Meal
 */
public function setId(int $id): Meal
{
    $this->id = $id;
    return $this;
}

/**
 * @return string
 */
public function getName(): string
{
    return $this->name;
}

/**
 * @param string $name
 * @return Meal
 */
public function setName(string $name): Meal
{
    $this->name = $name;
    return $this;
}

/**
 * @return Collection|ProductsQuantity[]
 */
public function getProductsQuantity()
{
    return $this->productsQuantity;
}

/**
 * @param Collection|ProductsQuantity[] $productsQuantity
 * @return Meal
 */
public function setProductsQuantity($productsQuantity)
{
    $this->productsQuantity = $productsQuantity;
    return $this;
}
/**
 * @Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 */
protected $id;

/**
 * @var Product
 * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
 * @ORM\ManyToOne(targetEntity="Product", inversedBy="productsQuantity")
 */
protected $product;

/**
 * @var integer
 * @ORM\Column(name="amount", type="integer")
 */
protected $amount;

/**
 * @var $meal
 * @ORM\ManyToMany(targetEntity="MealBundle\Entity\Meal", mappedBy="productsQuantity")
 */
protected $meal;


/**
 * @return mixed
 */
public function getId()
{
    return $this->id;
}

/**
 * @param mixed $id
 * @return ProductsQuantity
 */
public function setId($id)
{
    $this->id = $id;
    return $this;
}

/**
 * @return Product
 */
public function getProduct(): ?Product
{
    return $this->product;
}

/**
 * @param Product $product
 * @return ProductsQuantity
 */
public function setProduct(Product $product): ProductsQuantity
{
    $this->product = $product;
    return $this;
}

/**
 * @return int
 */
public function getAmount(): ?int
{
    return $this->amount;
}

/**
 * @param int $amount
 */
public function setAmount(int $amount): void
{
    $this->amount = $amount;
}

/**
 * @return mixed
 */
public function getMeal()
{
    return $this->meal;
}

/**
 * @param mixed $meal
 * @return ProductsQuantity
 */
public function setMeal($meal)
{
    $this->meal = $meal;
    return $this;
}
class MealType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options = [])
{
    parent::buildForm($builder, $options);

    /** @var Meal $meal */
    $meal = $options['data'];
    $data = null;

    if(!empty($meal)) {
        $data = $meal->getProductsQuantity();
    } else {
        $data = new ProductsQuantity();
    }

    $builder
        ->add('name', TextType::class,[
            'label' => 'Nazwa Dania',
            'required' => true
        ])->add('productsQuantity', CollectionType::class, [
            'data_class' => null,
            'label' => 'Produkty',
            'entry_type' => ProductsQuantityType::class,
            'allow_add' => true,
            'data' => ['productsQuantity' => $data],
            'prototype_name' => '__product__',
            'entry_options' => [
                'allow_extra_fields' => true,
                'label' => false
            ],
            'prototype' => true
        ])->add('addProduct', ButtonType::class, [
            'label' => 'Dodaj kolejny produkt',
            'attr'  => [
                'class' => 'btn-default addProductEntry'
            ]
        ])->add('submit', SubmitType::class, [
            'label' => 'Dodaj'
        ]);
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setRequired('productsQuantity');
    $resolver->setDefaults([
        'data_class' => Meal::class
    ]);
}
}
class ProductsQuantityType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);

    $builder
        ->add('product', EntityType::class,[
            'class' => Product::class,
            'label' => 'Nazwa produktu',

        ])
        ->add('amount', NumberType::class, [
            'label' => 'Ilość',
            'required' => true,
            'attr' => [
                'placeholder' => 'ilość'
            ]
        ])
        ->add('removeProduct', ButtonType::class, [
            'label' => 'X',
            'attr'  => [
                'class' => 'btn-danger removeProductEntry'
            ]
        ]);
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => ProductsQuantity::class,
    ]);
}
}