2017-05-09 8 views
0

現在Symfonyには、企業名の横にあるチェックボックスを持つ企業のリストがあります。これにより、各ユーザーに割り当てられている会社を確認することができます。チェックボックスには現在accountIDが表示されていますが、エンティティフィールドも 'name'にすると便利です。 2つのエンティティフィールドを持つプロパティを構築できますか?ここに私のフォームは私のコントローラである:symfonyで生成されたフォームのラベルを変更する

->add('companies', 'entity', array(
      'label'   => 'Company', 
      'class'   => 'Default\Bundle\Entity\Customer', 
      'property' => 'accountId', //This puts the company id next to the check box 
      'multiple'  => true, 
      'expanded'  => true, 
      'query_builder' => function ($repository) 
       { 
        return $repository->createQueryBuilder('c')->orderBy('c.accountId', 'ASC'); 
       },)) 
     ->add('Save', 'submit') 
     ->getForm(); 

これは私が何をしようとしていますものです:

->add('companies', 'entity', array(
     'label'   => 'Company', 
     'class'   => 'Default\Bundle\Entity\Customer', 
     'property' => 'accountId' + 'name', // I want to combine my entity fields here 
     'multiple'  => true, 
     'expanded'  => true, 
     'query_builder' => function ($repository) 

ここ実体は単なる参照

class Customer 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @Assert\NotBlank(message="Please enter a Customer ID.") 
    * @Assert\Length(max="32") 
    * @ORM\Column(type="string", length=32) 
    * @var string 
    */ 
    protected $accountId; 

    /** 
    * @Assert\NotBlank(message="Please enter a company name.") 
    * @Assert\Length(max="60") 
    * @ORM\Column(type="string", length=60) 
    * @var string 
    */ 
    protected $name; 

そして最後の一時間のためです。 ..私はこのから行きたい:

Original

この先:

New

答えて

2

例えば、簡単なゲッターを作成し、プロパティとしてそれを使用:

public function getNamePlusAccountId() 
{ 
    return $this->name." (".$this->accountId.")"; 
} 

とフォームで'property' => 'namePlusAccountId'を使用しています。

+0

驚くばかり探しているものをおそらくhttp://symfony.com/doc/current/reference/forms/types/entity.html#choice-label、ラベルを変更する必要がなく、フォームフィールドの値を保持したい場合!フォームを構築するときに関数を参照できるかどうかはわかりませんでした。私はそれがクラスの変数でなければならないと仮定した。 –

関連する問題