symfony2表单构建器从连接表中加载选项

时间:2016-01-20 01:42:40

标签: php symfony

我有两个表连接在一起,我想为一个表创建一个插入表单,从另一个表中添加选项。例如:

表感兴趣

namespace Nbois\CRMBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="Nbois\CRMBundle\Repository\InterestedRepository")
 * @ORM\Table(name="crm_interested")
 */
class Interested {

  /**
   * @ORM\Column(type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;



  //.....OTHER FIELDS


  /**
  * @ORM\ManyToOne(targetEntity="Sex", inversedBy="interested")
  * @ORM\JoinColumn(name="sex_id", referencedColumnName="id")
  */
  private $sex;

  public function getSex(){
    return $this->sex;
  }

  public function setSex(Sex $sex){
    $this->sex = $sex;
    return $this;
  }
  // .... GET AND SET METHODS
  // ....

}

表性

namespace Nbois\CRMBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="sex")
 */
class Sex {
  /**
   * @ORM\Column(type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @ORM\Column(type="string", length=255)
   * @Assert\NotBlank()
   */
  private $name;

  /**
  * @ORM\OneToMany(targetEntity="Interested", mappedBy="sex")
  */
  protected $interested;

  public function getId(){
    return $this->id;
  }

  public function getName(){
    return $this->id;
  }

  public function setName($name){
    $this->name = $name;
    return $this;
  }
}

我想添加一个新的感兴趣的客户端,表单生成器如下所示:

class InterestedType extends AbstractType {
  /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
      $builder
        ->add('firstName', TextType::class)
        ......
        ->add('sex', EntityType::class, array(
              'class' => 'NboisCRMBundle:Sex',
              'choice_label' => 'Sex',
              'placeholder' => 'Choose an option',
              'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('s')
                ->orderBy('s.name', 'ASC');
              }
          ));
     }

     public function configureOptions(OptionsResolver $resolver)
     {
         $resolver->setDefaults(array(
        'data_class' => 'Nbois\CRMBundle\Entity\Interested'
        ));
      }
}

在控制器中:

public function newAction(Request $request){

    $interested = new Interested();
    $form = $this->createForm(InterestedType::class, $interested);


    return $this->render('NboisCRMBundle:Default:newInterested.html.twig', array('form' => $form->createView()));
  }

当我渲染表单时,我收到此错误:

  

财产"性别"也不是其中一种方法" getSex()"," sex()",   " isSex()"," hasSex()"," __ get()"在课堂上存在并具有公共访问权限   " Nbois \ CRMBundle \实体\性别"

1 个答案:

答案 0 :(得分:2)

choice_label指的是不存在的性别中的“性别”

这样的事情应该有效:

          'choice_label' => 'name',

Docs on choice label

也许你只是想要一个普通的无聊标签,在这种情况下你要使用'label'而不是'choice_label'