使用Doctrine - MappingException的Symfony2提交表单

时间:2013-01-29 11:17:23

标签: php symfony pdo doctrine-orm propel

我使用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;
    }
}

2 个答案:

答案 0 :(得分:0)

您似乎尝试使用doctrine来保留Propel对象。

如果你为Model\Users类添加一些学说映射,那肯定不是你想要的,即使它在理论上是可能的。

你肯定想要坚持$user的状态:

if ($form->isValid()) {
    $user->save(); // propel object implements Active Record pattern, they can save themselves, no doctrine entityManager needed.
}

答案 1 :(得分:0)

您确定字段名称是firstNamelastName等等吗?通常,Propel会在大写的CamelCase中生成字段名称,因此我希望看到FirstNameLastName。如果字段名称不完全匹配,则Propel将不会分配值,从而导致空INSERT。您可以转储User字段名称列表,如下所示:

var_dump(BaseUserPeer::getFieldNames());