Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 在FOSUserBundle上记录注册时间和登录时间_Symfony_Fosuserbundle - Fatal编程技术网

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

Symfony 在FOSUserBundle上记录注册时间和登录时间,symfony,fosuserbundle,Symfony,Fosuserbundle,FOSUserbundle 当用户注册时,我想在用户表上记录诸如createdAt、UpdateAt、loginAt之类的数据 我想的是我应该把这些绳子放在哪里 我可以找到类似的参考资料 它表示覆盖/src/Acme/UserBundle/Controller/RegistrationController.php class RegistrationController extends BaseController { public function registerAction()

FOSUserbundle

当用户注册时,我想在用户表上记录诸如createdAt、UpdateAt、loginAt之类的数据

我想的是我应该把这些绳子放在哪里

我可以找到类似的参考资料

它表示覆盖/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(),
        ));
但我不知道如何使用formdata将数据插入表中

我是symfony2的新手。 我想我仍然会碰到symfony2的基本逻辑。
感谢您的友好回复。

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

看看这里,了解如何扩展该类:

我解决了这个问题

我添加了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;
    }
感谢您的查看和帮助。

您可以安装并使用时间标签功能

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

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

/**
 * @ORM\Column(type="datetime", nullable=true)
 * @Gedmo\Timestampable(on="update")
 */
protected $updatedAt;
不要忘记添加
使用Gedmo\Mapping\Annotation作为Gedmo以使注释生效”


这正是您使用prePersist和preUpdate手动执行的操作,但它节省了几行代码并使代码保持整洁。

我扩展了FOS\UserBundle\Entity\User,并创建了createdAt、UpdateAt和loginAt列。我可以从表单注册中插入用户行数据,但我希望在注册用户行时自动插入createdAt。
/**
 * @ORM\Column(type="datetime")
 * @Gedmo\Timestampable(on="create")
 */
protected $createdAt;

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