Note that there are some explanatory texts on larger screens.

plurals
  1. POsymfony 1.4 updating some object while (or after) saving another one related to it
    text
    copied!<p>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.</p> <p>I really don't know if this is possible at all...</p> <p>I'll post my code, as a reference:</p> <p>Schema:</p> <pre><code>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 </code></pre> <p>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...</p> <p>(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...</p> <p>Pic actions.class.php:</p> <pre><code> public function executeCreate(sfWebRequest $request) { $this-&gt;form = new PicForm(); $this-&gt;form-&gt;bind( $request-&gt;getParameter($form-&gt;getName()), $request-&gt;getFiles($form-&gt;getName()) ); if ($form-&gt;isValid()) { $this-&gt;pic = $form-&gt;save(); $entry = $this-&gt;pic-&gt;getEntry(); $entry-&gt;updateSetPic($this-&gt;pic); $this-&gt;redirect('entry_show', $entry); } } </code></pre> <p>Finally, I have the following 'updateSetPic' method in the lib/model/doctrine/Entry.class.php file:</p> <pre><code> public function updateSetPic(Pic $pic) { $this-&gt;setPic($pic); $this-&gt;save(); } </code></pre> <p>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?)</p> <p>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...</p> <p>Any ideas?</p> <p>Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload