【问题标题】:symfony2 generating obj from form typesymfony2 从表单类型生成对象
【发布时间】:2014-03-10 06:06:47
【问题描述】:

我编写了一个小 symfony2 应用程序,它允许您提出问题,每个问题至少有一个任务,并且由用户提出。

但目前我有以下错误

“属性“question”和方法“getQuestion()”、“isQuestion()”、“hasQuestion()”、“__get()”都不存在并且在类“NAMESPACE(Question)”中具有公共访问权限".:"

我真的不知道为什么会出现这个错误,如果有人能解释这个错误的原因会非常有帮助。 以及为什么 symfony 认为 Question Entity 应该有其中一种方法。

以下是课程:

问题类(实体),

class Question extends Post
{
/*
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(type="integer")
 */
protected $id;
/**
 * @ORM\ManyToMany(targetEntity="PLACEHOLDER")
 * @Assert\NotBlank(message="You should select at least one tag.")
 */
protected $tags;

/**
 * @var string
 * @ORM\Column(type="text")
 * @Assert\NotBlank(message="You have to input a heading.")
 */
protected $heading;

/**
 * @var string
 * 
 * @ORM\Column(type="text")
 * @Assert\NotBlank(message="You have to enter a question text.")
 */
protected $text;

/**
 * @var \DateTime
 * 
 * @ORM\Column(type="datetime")
 */
protected $date;

/**
 * 
 * @ORM\ManyToOne(targetEntity="PLACEHOLDER")
 */
protected $user;

/**
 * Constructor
 */
public function __construct()
{
    $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
// GETTERS / SETTERS .... 
}

问题类型类(表单),

class QuestionType extends AbstractType
{
    /**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('tags', 'entity', array( 'class'=>'fully qualified class name(tag)',
                                        'property'=>'name'))
        ->add('heading', 'text')
        ->add('question', 'textarea')
        ->add('submit', 'submit')
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'fully qualified class names (Question Entity)'
    ));
}


/**
 * @return string
 */
public function getName()
{
    return 'PLACEHOLDER';
}
}

以及问题控制器操作:

public function askQuestionAction(Request $request)
{

    $form = $this->createForm(new QuestionType, new Question);
    $form->handleRequest($request);

    if ($form->isValid()) {
        // persist
    }

    return array('form' => $form->createView());
}

【问题讨论】:

    标签: php forms symfony


    【解决方案1】:

    您正在尝试使用

    将表单绑定到 Question 实体
    $form = $this->createForm(new QuestionType, new Question);
    $form->handleRequest($request);
    

    您在表单上有一个questionhandleRequest 将尝试绑定到Question 实体。

    看看你的代码,->add('question', 'textarea') 不应该是->add('text', 'textarea')吗?

    【讨论】:

      【解决方案2】:

      这个PLACEHOLDER(Namespace to Question Entity) 和这个PLACEHOLDER(Namespace to Tag class)。这些应该是完全限定的类名

      例如。 MyVendor\MyBundle\Entity\MyEntity

      【讨论】:

      • 对不起,写这个问题时出错了,我的意思是完全限定的类名。在应用程序中它已经是完全限定的类名
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 2022-01-21
      • 2012-04-03
      • 2017-05-27
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多