我最近在我的网站中实现了FOSUserBundle作为登录过程。我希望它扩展Author类。因此,无论何时通过FOSUSerBundle注册新用户,都会在Author类中创建一个新条目。在Author类中,我设置了slug,createdAt和其他有用的参数。我要从FOSUserBundle传递给Authot实体的字段是“名称”字段。然后,我要层叠FOSUser实体,如果删除了它,请同时删除Author实体。
所以示意FOSUserBundle.username => Author.name
除了具有@ ORM / OneToOne关系外,我不知道如何实现此代码。有什么想法吗?
用户注册完成后,您必须手动插入作者。FOSUserBundle提供了一种方法来挂接诸如注册完成后的事件。您可以创建FOSUserEvents::REGISTRATION_COMPLETED
事件的侦听器,并在其中创建您的Author实体。
在此处查看文档:https : //github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/controller_events.md
例如:
services.yml:
services:
my_user_registration_service:
class: MyBundle\EventListener\MyUserRegistrationListener
arguments: [@doctrine.orm.entity_manager]
tags:
- { name: kernel.event_subscriber }
MyUserRegistrationListener:
namespace MyBundle\EventListener;
use Doctrine\ORM\EntityManager;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use MyBundle\Entity\Author;
class EventSubscriber implements EventSubscriberInterface
{
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_COMPLETED => 'addAuthor',
);
}
public function addAuthor(FilterUserResponseEvent $event)
{
$user = $event->getUser();
$author = new Author();
$author->setName($user->getUsername();
$this->em->persist($author);
$this->em->flush();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句