KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Short Story (Edit) It is possible to store an array instead of a mapped association. In Symfony2, this is fairly easy using the collection Field Type . For example, using this technique, you could store an array of text fields that populate an array events field. However, to update an array, there is a trick, and this trick is beautifully explained by @Vadim Ashikhman in the accepted answer. Long Story Sometimes it is useful and more efficient to store an array instead of a mapped association. However, once created, it remains complicated to update this Array if the size of that array does not change? Many people have a similar issue but nobody found a proper solution to this problem. A team can organise many events. These events are simply stored within an array using Doctrine instead of using a OneToMany association. Therefore, the entity Event is not mapped with Doctrine. Entity Event (not mapped with Doctrine) <?php namespace Acme\TestBundle\Entity; ... class Event { /** * @Assert\NotBlank */ private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } Entity Team <?php namespace Acme\TestBundle\Entity; ... /** * @ORM\Entity() * @ORM\HasLifecycleCallbacks * @ORM\Table(name="teams") */ class Team { /** * @ORM\Column(type="array") * @var array */ protected $events; public function addEvent($event) { if (!in_array($event, $this->events, true)) { $this->events[] = $event;
Tags (comma-separated)
Save Edits
Cancel