在symfony中注册后预填充数据库字段


pre populate database field after registration in symfony

我正在使用FOSuserbundle,并试图在注册时预先填充数据库中的字段。我正在尝试自动添加值,如"http://localhost:1337/digitalartlab/web/profile/{username}/checkin"。用户名将是这个字符串的唯一变量。我该怎么做?

用户.php

   /**
     * Set checkinurl
     *
     * @param string $checkinurl
     *
     * @return User
     */
    public function setCheckinurl($checkinurl)
    {
        $this->checkinurl = $checkinurl;
        return $this;
    }
    /**
     * Get checkinurl
     *
     * @return string
     */
    public function getCheckinurl()
    {
        return $this->checkinurl;
    }

registrationtype.php(我在注册表中添加了一些额外的字段)。

<?php
// src/AppBundle/Form/RegistrationType.php
namespace DigitalArtLabBundle'Form;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstname');
        $builder->add('lastname');
        $builder->add('address');
        $builder->add('zipcode');
        $builder->add('Expertises');
        $builder->add('Interesses');
        $builder->add('saldo', 'integer', array(
            'label' => 'Saldo',
            'data' => '0'
        ));
    }
    public function getParent()
    {
        return 'fos_user_registration';
    }
    public function getName()
    {
        return 'app_user_registration';
    }
}

registrationcontroller:(禁用的代码是我自己填充字段的尝试)

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();
        $authUser = false;
        if ($confirmationEnabled) {
            $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
            $route = 'fos_user_registration_check_email';
        } else {
            $authUser = true;
            /*$userdata = $this->get('fos_user.user_manager')->updateUser($user->getUsername());
            $namespace = 'localhost:1337/digitalartlab/web/profile';
            $userdata->setCheckinurl('http://'.$namespace.'/'.$user->getUsername().'/checkin');*/
            $route = 'fos_user_registration_confirmed';
        }
        $this->setFlash('fos_user_success', 'registration.flash.user_created');
        $url = $this->container->get('router')->generate($route);
        $response = new RedirectResponse($url);
        if ($authUser) {
            $this->authenticateUser($user, $response);
        }

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

我已经阅读了文档,但这一切都让我感到困惑。我希望有人能帮我弄清楚这是如何工作的。谢谢

您可以创建一个注册侦听器来执行此任务。请参阅此处的文档。

确保在services.yml:中为侦听器添加服务

services:
    your_bundle.registration_listener:
        class: YourBundle'EventListener'RegistrationListener
        tags:
            - { name: kernel.event_subscriber }

监听器:

use FOS'UserBundle'FOSUserEvents;
use FOS'UserBundle'Event'FormEvent;
use Symfony'Component'EventDispatcher'EventSubscriberInterface;
/**
 * RegistrationListener 
 *
 */
class RegistrationListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
        );
    }
    public function onRegistrationSuccess(FormEvent $event)
    {
       $user = $event->getForm()->getData();
       $userName = $user->getUserName();
       $checkInUrl = 'http://localhost:1337/digitalartlab/web/profile/' . $username;
       $user->setCheckinurl($checkInUrl);
    }
}