mercredi 12 août 2015

Symfony2 many-to-many checkboxes

I know that question answered a lot of times but i dont understand where is my problem. So I have simple many-to-many relations:

class Item
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */

private $id;
/**
 * @ORM\ManyToMany(targetEntity="Store", inversedBy="items", cascade={"persist"})
 * @ORM\JoinTable(name="itemlist",
 * joinColumns={@ORM\JoinColumn(name="item", referencedColumnName="id")},
 * inverseJoinColumns={@ORM\JoinColumn(name="store", referencedColumnName="id")}
 * )
 */
private $stores;
}

And Store entity:

/**
 * Store
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Store
{   
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="Item", mappedBy="stores", cascade={"persist"})
     */
    private $items;
}

And I want to assign items to stores via checkboxes.

So generate ItemType:

class ItemType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $entityManager = $options['em'];

        $builder
            //...
            ->add('stores', 'entity', array('class' => 'FoodBundle:Store', 'property' => 'title','multiple' => true, 'expanded' => true))
            ;
    }

And problems starts here.

First of all I'm getting all checkboxes checked, but itemlist relation table is empty.

Second I'm getting in this page errors:

Notice: Undefined index: targetToSourceKeyColumns in \vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php on line 1773

Warning: Invalid argument supplied for foreach() \vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php on line 1773

And finally I can't manage relations via checkboxes and getting:

Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\foodserv\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 605 and defined

I think there is problem in mapping, but this solution fully copied from this symfony2 many-to-many form checkbox




Aucun commentaire:

Enregistrer un commentaire