[英]Getting specific Data from a form symfony2
My problem is that I want to set a value to one field of an Entity and this value comes from a form. 我的问题是我想为一个实体的一个字段设置一个值,这个值来自一个表单。
The form is bound to another entity and this field of the form is named fmedida
. 表单绑定到另一个实体,该表单的字段名为fmedida
。
This is what I've tried 这就是我尝试过的
$hijo-> setFinicio(new \DateTime($form->getData()->getfmedida()));
But of course the syntax is not correct since I have this error message: 但当然语法不正确,因为我有这个错误信息:
"DateTime::__construct() expects parameter 1 to be string, object given" “DateTime :: __ construct()期望参数1为字符串,给定对象”
I advise you to var_dump the value that you get from $form->getData()->getfmedida()
and see what is happening. 我建议你var_dump你从$form->getData()->getfmedida()
获得的值,看看发生了什么。
When I tried getData(), it returns array on my side, so also try: 当我尝试getData()时,它会返回我的数组,所以也尝试:
$data = $form->getData();
$fmedida = $data['fmedida'];
$hijo-> setFinicio(new \DateTime($fmedida));
Also, DateTime creates DateTime objects from strings. 此外,DateTime从字符串创建DateTime对象。 Obviously, the value which you are getting from $form->getData()->getfmedida()
is not a string, it's a object. 显然,你从$form->getData()->getfmedida()
值不是字符串,它是一个对象。 If $form->getData()->getfmedida()
gives you a DateTime object, I can't see a point in using DateTime, so just 如果$form->getData()->getfmedida()
给你一个DateTime对象,我在使用DateTime时看不到一点,所以只是
$hijo-> setFinicio($form->getData()->getfmedida());
should be enough. 应该够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.