I'm trying to set up a multiple choice form with many to many relationship but I keep getting this Error message "Unable to transform value for property path "rozmiar": Expected an array." This is How my Entity looks like:
/**
* @var ArrayCollection
* @ORM\ManyToMany(targetEntity="Rozmiar")
* @ORM\JoinTable(
* name="buty__rozmiary",
* joinColumns={@ORM\JoinColumn(name="buty_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="rozmiar_id", referencedColumnName="id")}
* )
*/
private $rozmiar;
/**
* Set rozmiar
*
* @param ArrayCollection $rozmiar
* @return rozmiar
*/
public function setRozmiar($rozmiar)
{
$this->rozmiar = $rozmiar;
return $this;
}
/**
* @return ArrayCollection
*/
public function getRozmiar()
{
return $this->rozmiar;
}
public function __construct()
{
$this->rozmiar = new ArrayCollection();
}
And my form:
->add('rozmiar', ChoiceType::class, array(
'expanded' => true,
'multiple' => true,
'choices' => [35,36,37,38,39,40]
)
)
What should I do to get this to work? Thanks in advance for your help.
EDIT: I changed the part of my FORM to
->add('rozmiar', EntityType::class, array(
'class' => 'ShoeShopBundle:Buty',
'expanded' => true,
'multiple' => true,
'choices' => [35,36,37,38,39,40] ))
and now I'm getting "Warning: spl_object_hash() expects parameter 1 to be object, integer given" error. What is the proper way to define choices in this kind of field?
You need an EntityType field
->add('rozmiar', EntityType::class, array(
'class' => 'MyBundle:Rozmiar',
'multiple' => true,
'expanded' => true,
'choices' => $editedObject->getRozmiarChoices(),
))
with
$editedObject = $builder->getData();
be carefull to edit addRozmiar Method for the added rozmiar to know that new link :
public function addRozmiar(\MyBundle\Entity\Rozmiar $rozmiar)
{
if (!$this->rozmiar->contains($rozmiar)) {
$this->rozmiar->add($rozmiar);
$rozmiar->addButy($this);
}
return $this;
}
in Symfony2 I also often needed to add this value in the form, in order to force your method usage : 'by_reference' => false,
more infos : http://symfony.com/doc/current/reference/forms/types/entity.html
Edit :
Your choice list need to be an array or an arrayCollection of objects of class "MyBundle:Rozmiar". not integers. You can also use the 'query_builder' option instead of the 'choices' one if the list is not specificly related to your edited object.
Your list [35,36,37,38,39,40] shouldn't be hard coded in your form. You should put it in your entity if this is an enum, or add a boolean field in your other table in ordrer to use the query builder to find them.
I hope this will help :)
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.