Question

I made a web application with Symfony2, in which a Pais has an Arraycolletion of Suits:

Pais:

/**
 * @ORM\OneToMany(targetEntity="Acc\ApssBundle\Entity\Suit", mappedBy="pais", cascade={"persist", "remove"})
 * @Assert\Valid()
 */
protected  $suits;

/**
 * Constructor
 */
public function __construct()
{
    $elements = array(new Suit('Suit1'), new Suit('Suit2'), new Suit('Suit3'), new Suit('Suit4'), new Suit('Suit5'));

    $this->suits = new \Doctrine\Common\Collections\ArrayCollection( $elements);

}

Suit

/**
 * @ORM\ManyToOne(targetEntity=Acc\ApssBundle\Entity\Pais", inversedBy="suits")
 */
public $pais;

PaisSuitType

$builder->add('suits', 'collection', array(
              'options' => array('data_class' => 'Acc\ApssBundle\Entity\Suit'),
              'prototype' => true,
             ));

Controller:

 $paises = array($es = new Pais(), 
                 $it = new Pais(),
                 $mx = new Pais(), 
                 $br = new Pais()
           );
 foreach ($paises as $pais){
     $form[$i] = $this->createForm(new PaisType(),$pais);
     $forms[ 'form'.(string)$i ] = $form[$i]->createView() ;
     $i++;
 }

Twig template:

{% for suit in form0.suits %}
   <td align = "center">{{ form(suit) }}</td>   
{% endfor %}

The error occurs in the twig template.

Was it helpful?

Solution 2

I'm solve this with another class CheckType:

class CheckType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{

     $builder->add('check', 'checkbox',array('label'=> ' '));

}

public function getName()
{
    return 'check';
}
}

And asign the class in PaisSuitType:

 $builder->add('name','text')
 ->add('suits', 'collection', array(
        'options' => array('data_class' => 'Acc\ApssBundle\Entity\Suit'),
        'prototype' => true,
        'type' => new CheckType(),
        ))  ;

OTHER TIPS

Define a __toString method in your Suit class returning the name (use your attribute) for example :

public function __toString() {
    return (string)$this->xxx;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top