2013-04-13 54 views
1

FOSUserbundle记录寄存器时间和登录时间上FOSUserBundle

我想记录诸如createdAt,UpdateAt,loginAt上的用户表中,当用户寄存器中的数据。

我在想什么是我应该把这些绳子放在哪里。

我可以找到类似的参考

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_controllers.md

它说覆盖/src/Acme/UserBundle/Controller/RegistrationController.php

class RegistrationController extends BaseController 
{ 
    public function registerAction() 
    { 
     $form = $this->container->get('fos_user.registration.form'); 
     $formHandler = $this->container->get('fos_user.registration.form.handler'); 
     $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled'); 

     $process = $formHandler->process($confirmationEnabled); 
     if ($process) { 
      $user = $form->getData(); 

      /***************************************************** 
      * Add new functionality (e.g. log the registration) * 
      *****************************************************/ 
      $this->container->get('logger')->info(
       sprintf('New user registration: %s', $user) 
      ); 

      if ($confirmationEnabled) { 
       $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail()); 
       $route = 'fos_user_registration_check_email'; 
      } else { 
       $this->authenticateUser($user); 
       $route = 'fos_user_registration_confirmed'; 
      } 

      $this->setFlash('fos_user_success', 'registration.flash.user_created'); 
      $url = $this->container->get('router')->generate($route); 

      return new RedirectResponse($url); 
     } 

     return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
      'form' => $form->createView(), 
     )); 

但我不弄清楚怎样的方式你可以用表格数据插入数据

like

$post = $form->getData(); 
$post->setCreatedAt(new \DateTime()); 
$post->setUpdatedAt(new \DateTime()); 
$em = $this->getDoctrine()->getEntityManager(); 
$em->persist($post); 
$em->flush(); 

我是symfony2的新手。 我想我仍然碰到symfony2的基本逻辑。 谢谢你的回复。

回答

4

我解决了这个问题。

我已经加入HasLifecycleCallbacks 和 两个功能prePersist,更新前

中的Acme/UserBundle /实体/ user.php的

/** 
* @ORM\Entity 
* @ORM\Table(name="fos_user") 
* 
* @ORM\HasLifecycleCallbacks 
* 
*/ 
class User extends BaseUser 
{ 

    /** 
    * @ORM\PrePersist() 
    * 
    */ 

    public function prePersist() 
    { 
     $this->createdAt = new \DateTime; 
     $this->updatedAt = new \DateTime; 
    } 

    /** 
    * Hook on pre-update operations 
    * @ORM\PreUpdate() 
    */ 
    public function preUpdate() 
    { 
     $this->updatedAt = new \DateTime; 
    } 

感谢您查看和帮助我。

1

如果要在数据库中保存数据,请将User类扩展为FOS\UserBundle\Entity\User类。之后,您将能够添加其他实体字段。

看看这里了解如何扩展该类:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md

+0

我扩展了FOS \ UserBundle \ Entity \ User并创建了createdAt,UpdateAt,loginAt列。 我可以从表单注册中插入用户行数据, 但我想要在用户行注册时自动插入createdAt。 – whitebear

1

您可以安装StofDoctrineExtensionsBundle并使用时间戳功能。

然后,在你的用户实体类,你可以添加:

/** 
* @ORM\Column(type="datetime") 
* @Gedmo\Timestampable(on="create") 
*/ 
protected $createdAt; 

/** 
* @ORM\Column(type="datetime", nullable=true) 
* @Gedmo\Timestampable(on="update") 
*/ 
protected $updatedAt; 

不要忘了在顶部加入use Gedmo\Mapping\Annotation as Gedmo;的注释工作。

这正是你用prePersist和preUpdate手动完成的工作,但它保存了几行代码并保持整洁。