php - Update another entity field in current entity
When I submit a form (in a sonata admin) for an entity, I need to modify a field from another entity in the same entity (for some statistics), but can't figure how to do it.
Let's say I have a class with normal fields (title...) and a link with another entity "organization" :
/**
* Event
*
* @ORM\Table(name="event")
* @ORM\Entity(repositoryClass="...")
* @ORM\HasLifecycleCallbacks
* @Assert\Callback(methods={"isFormValid"})
*/
class Event
{
private $title;
...
/**
* @ORM\ManyToMany(targetEntity="Organization", cascade={"persist"})
* @ORM\JoinTable(name="event_organization",
* joinColumns={@ORM\JoinColumn(name="event_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="organization_id", referencedColumnName="id")}
* )
**/
private $organizations;
}
To modify a field in organization during the submit of Event, I do :
/**
* @ORM\PreUpdate
*/
public function preUpdate()
{
$organizations = $this->getOrganizations();
foreach($organizations as $orga) {
$orga->setTitle('test');
}
}
But it doesn't work, I also tried (for testing purpose) to add a new entity :
/**
* @ORM\PreUpdate
*/
public function preUpdate()
{
$entity = new Organization();
$entity->setTitle('New');
$entity->setCode('123456');
$this->organizations->add($entity);
}
It doesn't work either.
What should I do?