我有3个实体。 Meal
,Product
和ProductsQuantity
。 Meal
和ProductQuantity
之间的关系是多对多的。添加数据工作正常,所有数据都保存到实体,但是问题在于何时要编辑表单。然后我得到了错误:
该表单的视图数据应该是类
MealBundle\Entity\ProductsQuantity
的实例,但是是类Doctrine\ORM\PersistentCollection
的实例。您可以通过将"data_class"
选项设置为null
或通过添加将类Doctrine\ORM\PersistentCollection
的实例转换为MealBundle\Entity\ProductsQuantity
的实例的视图转换器来避免此错误。
我尝试将data_class
选项设置为null
,并在关系上设置了fetch="EAGER"
,但这并不能解决问题。
餐:
/**
* @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,
]);
}
}
如果我将'data' => ['productsQuantity' => $data]
更改为'data' => ['productsQuantity' => new ProductsQuantity()]
,则没有错误,但是ProductsQuantity
格式的MealType
部分为空。谁能告诉我如何解决这个问题?