php - Update another entity field in current entity

424

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?

People are also looking for solutions to the problem: proc open - can not get output when use php proc_open execute command?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.