【问题标题】:Object of class Acc\ApssBundle\Entity\Suit could not be converted to stringAcc\ApssBundle\Entity\Suit 类的对象无法转换为字符串
【发布时间】:2014-05-08 15:20:26
【问题描述】:

我用 Symfony2 制作了一个 Web 应用程序,其中一个 Pais 有一个 Arraycolletion of Suits:

派斯:

/**
 * @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++;
 }

树枝模板:

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

错误发生在树枝模板中。

【问题讨论】:

  • 与您当前的问题不同,您在 $pais 的 orm 中没有 JoinColumn

标签: php symfony checkbox entity


【解决方案1】:

在您的Suit 类中定义一个__toString 方法,返回name(使用您的属性),例如:

public function __toString() {
    return (string)$this->xxx;
}

【讨论】:

    【解决方案2】:

    我正在用另一个类 CheckType 解决这个问题:

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

    【讨论】:

      猜你喜欢
      • 2017-10-01
      • 2023-01-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2015-07-27
      • 2012-04-29
      • 2011-04-06
      相关资源
      最近更新 更多