Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php symfony2表单生成器从联接表加载选项_Php_Symfony - Fatal编程技术网

Php symfony2表单生成器从联接表加载选项

Php symfony2表单生成器从联接表加载选项,php,symfony,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\Interested

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

感兴趣的表格

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()));
  }
当我渲染表单时,会出现以下错误:

属性“Sex”或方法“getSex()”,“Sex()”, 类中存在并具有公共访问权限的“isSex()”、“hasSex()”、“\uu get()” “Nbois\CRMBundle\Entity\Sex”


choice_标签指的是sex中不存在的属性“sex”

像这样的方法应该会奏效:

          'choice_label' => 'name',


也可能你只是想要一个普通的无聊标签,在这种情况下,你会使用'label'而不是'choice\u label'

choice\u label指的是sex中不存在的属性'sex'

像这样的方法应该会奏效:

          'choice_label' => 'name',


也可能你只是想要一个普通的无聊标签,在这种情况下,你应该使用“标签”而不是“选择标签”

对不起,我已经编辑了我的问题。我有所有这些设置,但仍然得到那个错误。奇怪。没问题,你的小树枝模板(newinterest.html.twig)中的表单呈现是什么样子的?对不起,我已经编辑了我的问题。我有所有这些设置,但仍然得到那个错误。奇怪。没问题,你的表单呈现在你的小树枝模板(newinterest.html.twig)中是什么样子的?