我使用 Propel 创建了一个表单,它可以很好地提交并验证。当我尝试提交 $user 对象时出现问题 - 我得到一个 MappingException。我真的不知道这是从哪里来的,因为以前对 $user 的引用似乎很好。
请注意,注释行取自某些表单指南,但在数据库中插入了一个空行(尽管 $user 的 var_dump 显示它包含所有信息。如果我能将其用作选择。
这是我的代码:
namespace LifeMirror\APIBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use LifeMirror\APIBundle\Model\Users;
use LifeMirror\APIBundle\Model\UsersQuery;
use LifeMirror\APIBundle\Form\Type\UsersType;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use FOS\RestBundle\View\View;
class RegisterController extends Controller
{
public function indexAction()
{
return $this->processForm(new Users());
}
private function processForm(Users $user)
{
$statusCode = $user->isNew() ? 201 : 204;
$em = $this->getDoctrine()->getEntityManager();
$form = $this->createForm(new UsersType(), $user);
//die(phpinfo());
$form->bind(array(
"firstName" => $this->getRequest()->request->get('firstName'),
"lastName" => $this->getRequest()->request->get('lastName'),
"email" => $this->getRequest()->request->get('email'),
"password" => $this->getRequest()->request->get('password'),
"dob" => array(
"year" => json_decode($this->getRequest()->request->get('dob'))->year,
"month" => json_decode($this->getRequest()->request->get('dob'))->month,
"day" => json_decode($this->getRequest()->request->get('dob'))->day
),
"location" => $this->getRequest()->request->get('location'),
"tutorialWatched" => $this->getRequest()->request->get('tutorialWatched'),
"challengeEmails" => $this->getRequest()->request->get('challengeEmails'),
"mailingList" => $this->getRequest()->request->get('mailingList')
));
if ($form->isValid()) {
//$user->save();
$em->persist($user);
$em->flush();
$response = new Response();
$response->setStatusCode($statusCode);
return $response;
}
$view = View::create($form, 400);
$view->setFormat('json');
return $view;
}
}