小编JIG*_*GEN的帖子

当实体关系/关联时,Symfony2验证不起作用

调节器

public function indexAction(Request $request)
{
    $user = $this->container->get('security.context')->getToken()->getUser();
    $owner = $user->getId();

    $first = new First();
    $first->setOwner($owner);

    $second = new Second();
    $second->setOwner($owner);
    $second->setFirst($first);

    $form = $this->createForm(new SecondType(), $second);

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->get('doctrine')->getEntityManager();
            $em->persist($first);
            $em->persist($second);
            $em->flush();
        }
    }

    return $this->render('MySampleBundle:Home:index.html.twig', array(
        'form' => $form->createView(),
    ));

}
Run Code Online (Sandbox Code Playgroud)

ORM Yaml

My\SampleBundle\Entity\First:
    type: entity
    table: first
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        title:
            type: string
        date_created:
            type: datetime
        date_edited:
            type: datetime
        owner: …
Run Code Online (Sandbox Code Playgroud)

php validation entity associations symfony-2.1

7
推荐指数
1
解决办法
4027
查看次数

标签 统计

associations ×1

entity ×1

php ×1

symfony-2.1 ×1

validation ×1