我使用Symfony2创建了一个Web应用程序,其中Pais有一个适合的Arraycolletion:
派斯:
/**
* @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);
}
适合
/**
* @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,
));
控制器:
$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模板:
{% for suit in form0.suits %}
<td align = "center">{{ form(suit) }}</td>
{% endfor %}
树枝模板中发生错误。
答案 0 :(得分:0)
在__toString
类中定义Suit
方法,返回name
(使用您的属性),例如:
public function __toString() {
return (string)$this->xxx;
}
答案 1 :(得分:0)
我用另一个类检查类型:
来解决这个问题class CheckType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('check', 'checkbox',array('label'=> ' '));
}
public function getName()
{
return 'check';
}
}
并在PaisSuitType中分配类:
$builder->add('name','text')
->add('suits', 'collection', array(
'options' => array('data_class' => 'Acc\ApssBundle\Entity\Suit'),
'prototype' => true,
'type' => new CheckType(),
)) ;