在FOSUserBundle上记录注册时间和登录时间

whi*_*ear 2 symfony fosuserbundle

FOSUserbundle

我想在用户注册时记录User表上的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(),
        ));
Run Code Online (Sandbox Code Playgroud)

但我不知道如何使用formdata将数据插入表中

喜欢

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

我是symfony2的新手.我想我仍然在讨论symfony2的基本逻辑.谢谢你的回复.

whi*_*ear 5

我解决了这个问题.

我添加了HasLifecycleCallbacks和两个函数prePersist,preUpdate

在Acme/UserBundle/Entity/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;
    }
Run Code Online (Sandbox Code Playgroud)

感谢你观看并帮助我.