不要在Symfony2中保留现有数据

时间:2014-08-25 15:05:26

标签: php symfony

有没有办法避免使用Symfony2保持数据?这是我的例子:

我有一个用户和一个有一个表单的体验实体。我的体验实体与cascade={persist}的用户绑定。因此,当用户填写他的电子邮件地址和他的经验时,两个实体都会被创建并绑定。

我的问题是如果他的电子邮件地址已经存在,如何避免将用户信息发送到数据库? 这是我的功能:

public function participeAction(Request $request)
{
    $type = new EcoActorsType();
    $form = $this->createForm($type);
    $form->handleRequest($request);

    if ($form->isValid())
    {
        $data = $form->getData();

        $doctrine = $this->getDoctrine();
        $em = $doctrine->getManager();

        $email = $data->getUserActor();
        $email = $email->getEmail();

        $is_email = $em
            ->getRepository('Mobility\PublicBundle\Entity\UserActor')
            ->findOneBy(array(
                'email' => $email
            ));

        if ($is_email == null)
        {
            $em->persist($data);
            $em->flush();
        }
        else
        {
            ????
        }

        $url = $this->generateUrl('public.frontpages.participe');
        return $this->redirect($url);
    }

    return array(
        'form' => $form->createView()
    );
}

这是我的数据对象:

object(Mobility\PublicBundle\Entity\EcoActors)[905]
    private 'id' => null
    private 'title' => string 'test experience' (length=15)
    private 'type' => int 0
    private 'start' => string 'test start' (length=10)
    private 'arrival' => string 'test arrival' (length=12)
    private 'description' => string 'test test test' (length=14)
    private 'game' => boolean false
    private 'useractor' => 
        object(Mobility\PublicBundle\Entity\UserActor)[898]
            private 'id' => null
            private 'email' => string 'test@test.fr' (length=12)
            private 'ges' => int 2

3 个答案:

答案 0 :(得分:0)

$existingUser = $em->getRepository( "you entity shortname AcmeBundle:Entity" )->findOneByEmail( $email );

if ( $existingUser !== null ) 
{
  //User exists
}else
{
  //User doesn't exist

}

答案 1 :(得分:0)

在数据库完整性方面,您可以使用@UniqueConstraint让架构将列指定为唯一。

/**
 * @Entity
 * @Table(name="UserActor",uniqueConstraints={@UniqueConstraint(name="email_idx", columns={"email"})})
 */
class UserActor
{
    // ...
}

在Controller端,您使用带有Doctrine的findOneBy辅助函数(默认存储库类将为您实体中的每个列生成特殊的查找函数,例如findOneByEmail

$user = $this->getDoctrine()
    ->getRepository('MobilityPublicBundle:UserActor')->findOneByEmail($email);
if ($user) {
    // User exists
} else {
    // User doesn't exist
}

答案 2 :(得分:0)

最后很简单。我只是将对象设置为已存在于数据库中的对象。 这是新代码:

public function participeAction(Request $request)
{
    $type = new EcoActorsType();
    $form = $this->createForm($type);
    $form->handleRequest($request);

if ($form->isValid())
{
    $data = $form->getData();

    $doctrine = $this->getDoctrine();
    $em = $doctrine->getManager();

    $email = $data->getUserActor();
    $email = $email->getEmail();

    $is_email = $em
        ->getRepository('Mobility\PublicBundle\Entity\UserActor')
        ->findOneBy(array(
            'email' => $email
        ));

    if ($is_email == null)
    {
        $em->persist($data);
        $em->flush();
    }
    else
    {
        $data->setUseractor($is_email);
        $em->persist($data);
        $em->flush();
    }

    $url = $this->generateUrl('public.frontpages.participe');
    return $this->redirect($url);
}

return array(
    'form' => $form->createView()
);

}