SF2形式:错误既不是属性...也不是方法之一“获取" [英] SF2 form : error Neither the property ... nor one of the methods "get

查看:104
本文介绍了SF2形式:错误既不是属性...也不是方法之一“获取"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试与Symfony 2.4.1进行联系,并且出现以下错误:

I try to do a contact form with Symfony 2.4.1 and I have the following error :

Neither the property "contact" nor one of the methods "getContact()", "isContact()", "hasContact()", "__get()" exist and have public access in class "Open\OpcBundle\Entity\Contact". 

我理解错误本身,但是在SF2表单文档中或在Web上找不到解决该错误的资源:

I understand the error itself, but I can't find any resources to solve it in SF2 forms documentation or on the web:

控制器代码如下:

[..]

class OpcController extends Controller {
public function contactAction(Request $request) {      
  $contact = new Contact();

  $form = $this->createForm(new ContactType(), $contact);

  $form->handleRequest($request);

  return $this->render("OpenOpcBundle:Opc:contact.html.twig",
        array("formu" => $form->createView(),
        )
  );      
}   
}  

联系人实体看起来像这样:

The Contact Entity looks like this :

[...] 
class Contact {
  protected $nom;
  protected $courriel;
  protected $sujet;
  protected $msg;

public function getNom() {
  return $this->nom;
}

public function setNom($nom) {
  $this->nom = $nom;
}

public function getCourriel() {
  return $this->courriel;
}

public function setCourriel($courriel) {
  $this->courriel = $courriel;
}

public function getSujet() {
  return $this->sujet;
}

public function setSujet($sujet) {
  $this->sujet = $sujet;
}

public function getMsg() {
  return $this->msg;
}

public function setMsg($msg) {
   $this->msg = $msg;
}  
} 

以及Form类代码:

public function buildForm(FormBuilderInterface $builder, array $options) {
  $builder->add('contact');
  ->add('nom', 'text'))
    ->add('courriel', 'email')
    ->add('sujet', 'text')
          ->add('msg', 'textarea')
    ->add('submit', 'submit');
}

public function getName() {
  return "Contact";
}

public function setDefaultOptions(OptionsResolverInterface $resolver) {
  $resolver->setDefaults(array('data_class' => 'Open\OpcBundle\Entity\Contact', ));
}
} 

我的错误在哪里?谢谢

推荐答案

您的错误是正确的,并告诉您实体Contact在您的buildForm()中不具有contact属性并且没有相关的getter setter方法.您使用了$builder->add('contact');这样的联系人属性,但是实体中不存在任何相关属性,请先在您的实体中定义该属性

Your error is correct and telling you that you entity Contact does not have the contact property and no related getter setter method while in your buildForm() you have used contact property like $builder->add('contact'); but there is no related property exists in the entity,Define the property first in your entity

class Contact {
  protected $nom;
  protected $courriel;
  protected $sujet;
  protected $msg;
  protected $contact;

public function getContact() {
  return $this->contact;
}

public function setContact($contact) {
  $this->contact= $contact;
}
/* ...
remaining methods in entity 

*/
}

,或者如果它是非映射字段,那么您必须在构建器中将该字段定义为非映射

or if its a non mapped field then you have to define this field in builder as non mapped

$builder->add('contact','text',array('mapped'=>false));

通过上面的定义,您将不需要更新您的实体

By defining above you will not need to update your entity

这篇关于SF2形式:错误既不是属性...也不是方法之一“获取"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆