简体   繁体   中英

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. I want to add a collection of "PackItem".

However,only the last item is added and when I add "by_reference" => false I've got this issue

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
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

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().

You can check this page for 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. Now I've got an other problem, only the last Item is saved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM