"gedmo/doctrine-extensions": "dev-master"
services: gedmo.listener.reference: class: Gedmo\References\ReferencesListener tags: - { name: doctrine_mongodb.odm.event_subscriber } calls: - [ setAnnotationReader, [ "@annotation_reader" ] ] - [ registerManager, [ 'entity', "@doctrine.orm.default_entity_manager" ] ] utils.listener.reference: class: Utils\ReferenceBundle\Listener\ReferencesListener arguments: ["@service_container"] tags: - { name: doctrine.event_subscriber, connection: default }
<?php namespace Utils\ReferenceBundle\Listener; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Class ReferencesListener * * @package Utils\ReferenceBundle\Listener */ class ReferencesListener extends \Gedmo\References\ReferencesListener { /** * @var \Symfony\Component\DependencyInjection\ContainerInterface */ private $container; /** * @var array */ protected $managers = [ 'document' => 'doctrine.odm.mongodb.document_manager', 'entity' => 'doctrine.orm.default_entity_manager' ]; /** * @param ContainerInterface $container * @param array $managers */ public function __construct(ContainerInterface $container, array $managers = array()) { $this->container = $container; parent::__construct($managers); } /** * @param $type * * @return object */ public function getManager($type) { return $this->container->get($this->managers[$type]); } }
/*Entity\User:*/ /** * @var ArrayCollection * * @Gedmo\ReferenceMany(type="document", class="\Application\Sonata\UserBundle\Document\Address", mappedBy="user") */ protected $addresses;
/*Document\Address:*/ /** * @Gedmo\ReferenceOne(type="entity", class="\Application\Sonata\UserBundle\Entity\User", inversedBy="addresses", identifier="user_id", mappedBy="user_id") */ protected $user; /** * @var int $user_id */ protected $user_id;
$user = new User(); $address = new Address(); $address->setAddress(«aaa»); $address->setUser($user); $user->getAddresses()->add($address); $em->persist($user); $em->flush();
protected function configureFormFields(FormMapper $formMapper) { $formMapper ->with('General') // … ->add('addresses', 'collection', array('type' => new AddressType(), 'allow_add' => true, 'by_reference' => false, 'allow_delete' => true)) ->end(); }
class AddressType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('firstname') ->add('lastname') ->add('address') ; } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Application\Sonata\UserBundle\Document\Address' )); } /** * @return string */ public function getName() { return 'application_sonata_userbundle_address'; // .... */
public function setAddresses($addresses) { foreach ($this->addresses as $orig_address) { // - — if (false === $addresses->contains($orig_address)) { // $this->addresses->removeElement($orig_address); } } // , , . foreach($addresses as $passed_address) { if(!$this->addresses->contains($passed_address)) { $passed_address->setUser($this); $this->addresses->add($passed_address); } } }
public function addAddress($addresses) { $addresses->setUser($this); $this->addresses[] = $addresses; return $this; }
public function postUpdate($user) { $dm = $this->container->get("doctrine_mongodb")->getManager(); $dbAddresses = $dm->getRepository('Application\Sonata\UserBundle\Document\Address')->findBy(array('user_id'=>$user->getId())); foreach($dbAddresses as $dbAddress) { if(!$user->getAddresses()->contains($dbAddress)) { echo $dbAddress->getFirstName(); $dm->remove($dbAddress); } } foreach($user->getAddresses() as $address) { $address->setUser($user); $dm->persist($address); } $dm->flush(); }
sonata.user.admin.user: class: %sonata.user.admin.user.class% tags: - { name: sonata.admin, manager_type: orm, group: %sonata.user.admin.groupname%, label: users, label_catalogue: SonataUserBundle, label_translator_strategy: sonata.admin.label.strategy.underscore } arguments: - ~ - %sonata.user.admin.user.entity% - %sonata.user.admin.user.controller% calls: - [ setUserManager, [@fos_user.user_manager]] - [ setTranslationDomain, [%sonata.user.admin.user.translation_domain%]] - [ setContainer, [@service_container]]</code> <code>- [ setContainer, [@service_container]]
/** @var \Symfony\Component\DependencyInjection\ContainerInterface */ private $container; public function setContainer (\Symfony\Component\DependencyInjection\ContainerInterface $container) { $this->container = $container; }
Source: https://habr.com/ru/post/228371/
All Articles