symfony 1.4 updating some object while (or after) saving another one related to it

2.7k Views Asked by At

I have a doctrine form in Symfony, which saves certain object in a table in the DB. By doing this, I wish to also update another object, which has a relation to the first one, so the objects get the relation (one-to-many) updated as the first object gets saved.

I really don't know if this is possible at all...

I'll post my code, as a reference:

Schema:

Entry:
  columns:
    pic: { type: integer, notnull: false, default: null }
  relations:
    Pic:
      local:   pic
      foreign: id
      onDelete: SET NULL
    Pics:
      type:    many
      class:   Pic
      local:   id
      foreign: entry_id


Pic:
  columns:
    id:         { type: integer, notnull: true, primary: true, autoincrement: true }
    entry_id:   { type: integer, notnull: true, primary: true }
    pic:        { type: string(255), notnull: true }
  relations:
    Entry:
      local:    entry_id
      foreign:  id
      onDelete: CASCADE

I have another many-to-many relation between Pic and Entry, because an Entry may have a lot of Pics, but the first Pic I create must also be defined as the 'default' Pic which I stablish in the Entry.pic field...

(I previously and correctly create an Entry, so now I must get a new Pic, which will be the 'default' one for the Entry)... In the create actions for the Pic table, I wish to update the Entry so it knows the brand new Pic I have just added and that should get linked by 'default' with it...

Pic actions.class.php:

  public function executeCreate(sfWebRequest $request)
  {
    $this->form = new PicForm();
    $this->form->bind(
                $request->getParameter($form->getName()),
                $request->getFiles($form->getName())
                );

    if ($form->isValid())
      {
        $this->pic = $form->save();
        $entry = $this->pic->getEntry();
        $entry->updateSetPic($this->pic);
        $this->redirect('entry_show', $entry);
      }
  }

Finally, I have the following 'updateSetPic' method in the lib/model/doctrine/Entry.class.php file:

  public function updateSetPic(Pic $pic)
  {
    $this->setPic($pic);
    $this->save();
  }

The problem I have right now is that, seemingly, the upateSetPic method, receiving the recently saved Pic object (in the Pic actions create method), just receives nothing, as if the Pic object that the $form->save() method has nothing in it (even though, it truly saves, but maybe at this stage it isn't commited or something?)

So my question is, am I doing this all right? Or where am I wrong? Is there a way to acomplish what I need? If so, what is that way? Maybe I need to mingle into another part of the Pic save process, but I'm kind of lost by now...

Any ideas?

Thank you!

1

There are 1 best solutions below

1
On

In your "$form->isValid()" if statement, it seems your $entrada, that you're calling updateSetPic on, is not set.

But to answer your question, you might want to look at the postSave() function you can add to your object ( http://www.doctrine-project.org/projects/orm/1.2/docs/manual/event-listeners/en ).