I don't know if this is the normal behavior, I have a OnetoMany relation between two class (Competitions and Event). I follow the documentation and I can create elements but I want to add elements to the relation and keep the other ones but I am not able to do it.
Competition Entity
/**
* Competitions able to request in the unibet API.
*
* @ApiResource(
* attributes={
* "normalization_context"={"groups"={"read", "write"}},
* "denormalization_context"={"groups"={"read","write"}}
* },
* collectionOperations={"get", "post"},
* itemOperations={"get", "put"}
* )
* @ORM\Entity
*/
class Competition
{
...
/**
* @ORM\OneToMany(targetEntity="Event", mappedBy="competitions", cascade={"persist"})
* @ApiSubresource(maxDepth=1)
* @Groups({"read","write"})
*/
public $events;
public function addEvent(Event $event)
{
$event->competition = $this;
$this->events->add($event);
}
public function removeEvent(Event $event)
{
$event->competition = null;
$this->events->removeElement($event);
}
I don't know if this is the normal behavior, I have a OnetoMany relation between two class (Competitions and Event). I follow the documentation and I can create elements but I want to add elements to the relation and keep the other ones but I am not able to do it.
Competition Entity
When I send a PUT notification to /competitions/{id}
It works perfectly, but if after this one I send another request to add other event in the same competition. The last event is removed:
Right now I only have in the database a relation with event 39. Is it possible to add elements to a relationship without creating a custom controller?