php SF2 形式:错误既不是属性...也不是方法之一“get
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22593677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SF2 form : error Neither the property ... nor one of the methods "get
提问by user3049922
I try to do a contact form with Symfony 2.4.1 and I have the following error :
我尝试使用 Symfony 2.4.1 制作联系表格,但出现以下错误:
Neither the property "contact" nor one of the methods "getContact()", "isContact()", "hasContact()", "__get()" exist and have public access in class "Open\OpcBundle\Entity\Contact".
I understand the error itself, but I can't find any resources to solve it in SF2 forms documentation or on the web:
我理解错误本身,但我在 SF2 表单文档或网络上找不到任何资源来解决它:
The controller code looks like this:
控制器代码如下所示:
[..]
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;
}
}
And the Form class code:
和 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', ));
}
}
Where is my mistake? Thanks
我的错误在哪里?谢谢
回答by M Khalid Junaid
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
您的错误是正确的,并告诉您您的实体Contact
没有该contact
属性并且没有相关的 getter setter 方法,而在您buildForm()
使用联系人属性时,例如$builder->add('contact');
但实体中不存在相关属性,请先在您的实体中定义该属性
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
通过上面定义,您将不需要更新您的实体