[英]setting 'by reference' to false, on symfony form throws 'Could not determine access type'
I'm trying to create a form for the creation of a product in sylius. 我正在尝试创建一个用于在sylius中创建产品的表单。 I want to add a collection of "PackItem".
我想添加一个“PackItem”的集合。
However,only the last item is added and when I add "by_reference" => false
I've got this issue 但是,只添加了最后一项,当我添加
"by_reference" => false
我遇到了这个问题
Could not determine access type for property "products".
无法确定属性“产品”的访问类型。
This is my code 这是我的代码
#ProductTypeExtension.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
/** @var PackItem $packItem */
$packItem = new PackItem();
$packItem->setParent($builder->getData());
$builder
->add('products', CollectionType::class, [
'entry_type' => PackItemType::class,
'allow_add' => true,
'allow_delete' => true,
'entry_options' => [
'data' => $packItem
],
'by_reference' => false,
]);
}
/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return ProductType::class;
}
PackItemType.php PackItemType.php
#PackItemType.php
final class PackItemType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('child', 'entity', [
'label' => 'winzana.ui.token',
'class' => Product::class,
'query_builder' => function(EntityRepository $er) {
$qr = $er->createQueryBuilder('t')
->leftJoin('t.products', 'p')
->having('COUNT(p.parent) = 0')
->groupBy('t.id')
->orderBy('t.code', 'ASC')
;
return $qr;
}
])
->add('quantity', IntegerType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => PackItem::class
]);
}
Product : 产品:
class Product extends BaseProduct
{
/**
* @ORM\OneToMany(targetEntity="XXXX\PackBundle\Entity\PackItem", mappedBy="parent", cascade={"persist"})
* @var ArrayCollection|PackItem $products
*/
private $products;
Thank you for your time 感谢您的时间
You can try to initialise your products in the __construct() method of your class product 您可以尝试在类产品的__construct()方法中初始化产品
public function __construct()
{
$this->products= new ArrayCollection();
}
if it does not correct the problem, then check if you set correctly the getProducts(), setProducts() and addProduct(). 如果它不能解决问题,那么检查你是否正确设置了getProducts(),setProducts()和addProduct()。
You can check this page for information, http://symfony.com/doc/current/best_practices/business-logic.html#doctrine-mapping-information 您可以查看此页面以获取相关信息, http://symfony.com/doc/current/best_practices/business-logic.html#doctrine-mapping-information
regards. 问候。
The problem was solved by this change 这个改变解决了这个问题
/**
* @param ArrayCollection|PackItem[] $products
*/
public function setProducts($products)
{
$this->products = $products;
}
I don't use the setter so I didn't made it however by_references
needs it. 我不使用setter所以我没有使用它,但是
by_references
需要它。 Now I've got an other problem, only the last Item is saved. 现在我遇到了另一个问题,只保存了最后一个项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.