Acc \ ApssBundle \ Entity \ Suit类的对象无法转换为字符串

时间:2014-05-08 15:20:27

标签: php symfony checkbox entity

我使用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 %}

树枝模板中发生错误。

2 个答案:

答案 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(),
        ))  ;