FosRest:使用put方法

时间:2015-11-25 16:02:28

标签: symfony fosrestbundle

我正在使用PUT方法,该方法允许用户更新其个人资料。我的问题是当我尝试更新用户实体中的birthDate字段时:

/**
 * @var \DateTime
 *
 * @ORM\Column(name="birth_date", type="date", nullable=true)
 *
 * @Expose
 */
private $birthDate;

使用getter和setter:

/**
 * Set birthDate
 *
 * @param \DateTime $birthDate
 * @return User
 */
public function setBirthDate(\DateTime $birthDate)
{
    $this->birthDate = $birthDate;

    return $this;
}

/**
 * Get birthDate
 *
 * @return \DateTime 
 */
public function getBirthDate()
{
    return $this->birthDate;
}

这是我的控制器代码示例:

$user = $this->getDoctrine()->getRepository('FTUserBundle:User')->find($user_id);

if(null == $user) {
    $view = View::create("User not found", Codes::HTTP_NOT_FOUND);
    return $this->handleView($view);
}

$form = $this->createForm(new UserProfileType(), $user, array('method' => 'PUT'));

当调用createForm时,错误就在这里:

{"error":{"code":500,"message":"Internal Server Error","exception":[{"message":"Unable to transform value for property path \"birthDate\": datefmt_format: string '' is not numeric, which would be required for it to be a valid date: U_ILLEGAL_ARGUMENT_ERROR"

这是我的UserProfileType

$builder->add('birthDate', 'date', array(
    'widget'   => 'single_text',
    'format' => 'dd-MM-yyyy'
));

在数据库中正确设置了birthDate。如果我用静态birthDate对象替换DateTime getter的返回值,它可以正常工作。当我在构建表单之前调用$user->getBirthDate()时,它会返回一个正确的DateTime对象。

1 个答案:

答案 0 :(得分:0)

我的错误在于我的UserProfileType()。 birthDate字段类型必须是" datetime",而不是" date"。