ठीक है, मैंने artworkad के विचार को लागू करने पर काम करना शुरू कर दिया।
पहली बात मैंने किया था FOSUserBundle को 2.0.*@dev को composer.json में अपडेट कर रहा था, क्योंकि मैं v1.3.1 का उपयोग कर रहा था, जो FOSUserEvents क्लास को लागू नहीं करता है। मेरे पंजीकरण कार्यक्रम की सदस्यता लेने के लिए यह आवश्यक है।
// composer.json
"friendsofsymfony/user-bundle": "2.0.*@dev",
तो मैं एक नई सेवा कहा:
<!-- Moskito/Bundle/UserBundle/Resources/config/services.xml -->
<service id="moskito_bundle_user.user_creation" class="Moskito\Bundle\UserBundle\EventListener\UserCreationListener">
<tag name="kernel.event_subscriber" alias="moskito_user_creation_listener" />
<argument type="service" id="doctrine.orm.entity_manager"/>
</service>
एक्सएमएल में, मैं सेवा बताया कि मैं एक तर्क doctrine.orm.entity_manager
के माध्यम से सिद्धांत के लिए उपयोग की जरूरत है। फिर, मैंने श्रोता बनाया:
// Moskito/Bundle/UserBundle/EventListener/UserCreationListener.php
<?php
namespace Moskito\Bundle\UserBundle\EventListener;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityManager;
/**
* Listener responsible to change the redirection at the end of the password resetting
*/
class UserCreationListener implements EventSubscriberInterface
{
protected $em;
protected $user;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
);
}
public function onRegistrationSuccess(FormEvent $event)
{
$this->user = $event->getForm()->getData();
$group_name = 'my_default_group_name';
$entity = $this->em->getRepository('MoskitoUserBundle:Group')->findOneByName($group_name); // You could do that by Id, too
$this->user->addGroup($entity);
$this->em->flush();
}
}
और मूल रूप से, यह है!
प्रत्येक पंजीकरण सफलता के बाद, onRegistrationSuccess()
कहा जाता है, इसलिए मुझे उपयोगकर्ता को FormEvent $event
के माध्यम से मिलता है और इसे मेरे डिफ़ॉल्ट समूह में जोड़ता है, जिसे मैं सिद्धांत के माध्यम से प्राप्त करता हूं।
आपके उत्तर का दूसरा भाग ऐसा लगता है कि मैं क्या करना चाहता हूं। मैं इसे लागू करने की कोशिश करूंगा और यहां एक समाधान के रूप में उत्तर पोस्ट करूंगा। – Weengs