Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 8 characters in body
Source Link
Will B.
  • 18.5k
  • 5
  • 72
  • 73

This allows you to use solely rely solely on the Doctrine ORM Entity Manager to maintain the default Group association.

In Symfony 3.4+ you can use Repository services to provide dependency injection for the repository, instead of using the entity managerEntityManagerInterface.

This allows you to use solely rely on the Doctrine ORM to maintain the default Group association.

In Symfony 3.4+ you can use Repository services to provide dependency injection for the repository, instead of using the entity manager.

This allows you to rely solely on the Doctrine ORM Entity Manager to maintain the default Group association.

In Symfony 3.4+ you can use Repository services to provide dependency injection for the repository, instead of using the EntityManagerInterface.

deleted 1 character in body
Source Link
Will B.
  • 18.5k
  • 5
  • 72
  • 73

This approach can be extended upon in Symfony to use Query services instead of the doctrine entity repository, to give youprovide a central location that handles the instantiation of the entities.

In Symfony 3.4+ you can use Repository services to provide dependency injection for the repository, instead of using the entity manager.

Note: Usages of $em->getReference() can be replaced with $em->find(). Using $em->getReference() will prevent a query to the database but will throw an exception if the reference is invalid, wherewhile using $em->find() will return null instead.

This approach can be extended upon in Symfony to use Query services instead of the doctrine entity repository, to give you a central location that handles the instantiation of the entities.

Note: Usages of $em->getReference() can be replaced with $em->find(). Using $em->getReference() will prevent a query to the database but will throw an exception if the reference is invalid, where using $em->find() will return null.

This approach can be extended upon in Symfony to use Query services instead of the doctrine entity repository, to provide a central location that handles the instantiation of the entities.

In Symfony 3.4+ you can use Repository services to provide dependency injection for the repository, instead of using the entity manager.

Note: Usages of $em->getReference() can be replaced with $em->find(). Using $em->getReference() will prevent a query to the database but will throw an exception if the reference is invalid, while using $em->find() will return null instead.

added 37 characters in body
Source Link
Will B.
  • 18.5k
  • 5
  • 72
  • 73

This allows you to use doctrinesolely rely on the Doctrine ORM to managemaintain the default groupGroup association.

use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\HasLifecycleCallbacks */ class Person { const DEFAULT_GROUP = 122; /** @ORM\Column(type="string") */ private $name = '';'Mike'; /** * @ORM\ManyToOne(targetEntity="Group", inversedBy="persons") * @ORM\JoinColumn(referencedColumnName="id") */ private $group; //.... public function setGroup(Group $group) { $this->group = $group; $group->addPerson($this); } /** * @param LifecycleEventArgs $event * @ORM\PrePersist */ public function onPrePersist(LifecycleEventArgs $event) { if (!$this->group instanceof Group) { /** set default group if not specified */ $group = $event->getEntityManager()->getReference(Group::class, self::DEFAULT_GROUP); $this->setGroup($group); } } } 

This allows you to use doctrine to manage the default group.

use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\HasLifecycleCallbacks */ class Person { const DEFAULT_GROUP = 122; /** @ORM\Column(type="string") */ private $name = ''; /** * @ORM\ManyToOne(targetEntity="Group", inversedBy="persons") * @ORM\JoinColumn(referencedColumnName="id") */ private $group; //.... public function setGroup(Group $group) { $this->group = $group; $group->addPerson($this); } /** * @param LifecycleEventArgs $event * @ORM\PrePersist */ public function onPrePersist(LifecycleEventArgs $event) { if (!$this->group instanceof Group) { /** set default group if not specified */ $group = $event->getEntityManager()->getReference(Group::class, self::DEFAULT_GROUP); $this->setGroup($group); } } } 

This allows you to use solely rely on the Doctrine ORM to maintain the default Group association.

use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\HasLifecycleCallbacks */ class Person { const DEFAULT_GROUP = 122; /** @ORM\Column(type="string") */ private $name = 'Mike'; /** * @ORM\ManyToOne(targetEntity="Group", inversedBy="persons") * @ORM\JoinColumn(referencedColumnName="id") */ private $group; //.... public function setGroup(Group $group) { $this->group = $group; $group->addPerson($this); } /** * @param LifecycleEventArgs $event * @ORM\PrePersist */ public function onPrePersist(LifecycleEventArgs $event) { if (!$this->group instanceof Group) { /** set default group if not specified */ $group = $event->getEntityManager()->getReference(Group::class, self::DEFAULT_GROUP); $this->setGroup($group); } } } 
added 42 characters in body
Source Link
Will B.
  • 18.5k
  • 5
  • 72
  • 73
Loading
added recommended methods
Source Link
Will B.
  • 18.5k
  • 5
  • 72
  • 73
Loading
edited body
Source Link
Will B.
  • 18.5k
  • 5
  • 72
  • 73
Loading
added 1116 characters in body
Source Link
Will B.
  • 18.5k
  • 5
  • 72
  • 73
Loading
added 1116 characters in body
Source Link
Will B.
  • 18.5k
  • 5
  • 72
  • 73
Loading
updated links
Source Link
Will B.
  • 18.5k
  • 5
  • 72
  • 73
Loading
added default string value to demonstrate the lack of need to call __construct
Source Link
Will B.
  • 18.5k
  • 5
  • 72
  • 73
Loading
Source Link
Will B.
  • 18.5k
  • 5
  • 72
  • 73
Loading