Reputation: 742
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
UPD: Store setters in Item entity
/**
* Add store
*
* @param \FoodServiceBundle\Entity\Store $store
*
* @return Item
*/
public function addStore(\FoodServiceBundle\Entity\Store $store)
{
$this->stores[] = $store;
return $this;
}
/**
* Remove store
*
* @param \FoodServiceBundle\Entity\Store $store
*/
public function removeStore(\FoodServiceBundle\Entity\Store $store)
{
$this->stores->removeElement($store);
}
/**
* Get stores
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getStores()
{
return $this->stores;
}
Upvotes: 3
Views: 671
Reputation: 3523
Remove @ORM\JoinTable
from your Item entity like so:
class Item
{
//..
/**
* @ORM\ManyToMany(targetEntity="Store", inversedBy="items", cascade="persist"})
*/
private $stores;
}
Doctrine provides sensible default values for the join tables, and you override those with something that doesn't work. By removing @ORM\JoinTable
it should work again.
Unfortunately I never use the annotation mapping and can't tell you where exactly the error was.
Here is how to use @ORM\JoinTable
if you need non-default table or column names: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#many-to-many-self-referencing
Upvotes: 2