0

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

2 回答 2

0

你确定你的字段名称是firstNamelastName等等?通常,Propel 以大写的 CamelCase 生成字段名称,因此我希望看到FirstNameand LastName。如果字段名称不完全匹配,则 Propel 将不会分配值,从而导致空的INSERT. 您可以像这样转储User字段名称列表:

var_dump(BaseUserPeer::getFieldNames());
于 2013-01-29T18:34:39.820 回答
0

看来您尝试使用教义来持久化 Propel 对象。

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

您肯定想要的是保持$user's 状态:

if ($form->isValid()) {
    $user->save(); // propel object implements Active Record pattern, they can save themselves, no doctrine entityManager needed.
}
于 2013-01-29T13:48:49.337 回答