簡體   English   中英

以symfony2形式添加多個屬性

[英]add more than one property in a form symfony2

我有一個表單類型,在其中我將添加一個實體並添加多個屬性

$builder ->add('number', 'entity', array(
              'class'    => 'TelnOperatorBundle:Numberrange',
              'property' => 'De'
              'multiple' => false,));

在屬性中,我想添加另一個字段,即

$builder ->add('number', 'entity', array(
              'class'    => 'TelnOperatorBundle:Numberrange',
              'property' => 'De','A'
              'multiple' => false,));

我該怎么辦?

->add('subnumbers', 'collection', array('type'         => new SubnumberType(),
                                              'allow_add'    => true,
                                              'allow_delete' => true))

您不能,屬性僅允許將string作為參數: http : //symfony.com/doc/master/reference/forms/types/entity.html#property

您的解決方案是將property留為空白,然后在Entity中實現__toString()方法。

首先更改定義表單類型的方式(將空白值傳遞給屬性):

$builder ->add('number', 'entity', array(
              'class'    => 'TelnOperatorBundle:Numberrange',
              'multiple' => false,));

其次,在TelnOperatorBundle:Numberrange實體中定義__toString()函數:

public function __toString()
{
    return $this->De . ' : ' . $this->A;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM