views:

78

answers:

2

Hello,

I have the base table called SnUser and a related table, SnAltceva

schema.yml

SnUser: columns: name: { type: string(100) } age: { type: integer(1) } level_id: integer relations: SnAltceva: { local: id, foreign: user_id }

and

SnAltceva: columns: user_id: integer field_1: string(10) field_2: integer(1) relations: SnUser: local: user_id foreign: id onDelete: CASCADE

I created SnUser form and embedded it with a new SnAltceva form. Everything works well, sn_user table is updated with the new record, also the sn_altceva but the user_id is NULL.

My wish is to update this field also with the newly inserted if from SnUser. I suspect I need to override save() from /lib/model/doctrine/SnUser.class.php but not quite sure how.

Any suggestions? Thanks

A: 

You need to override save to fist save the user object. then get the object from the embedded form for Altceva and set the user id on it. then save that object.

sfGuardPlugins (both Doctrine and Propel) ave a good examle of this when it comes to User/UserProfile saves. You can use that as a reference.

prodigitalson
When SnUser is saved (parent::save($con)), a record for SnAltceva is inserted as well in db (except user_id field). If I get the newly id and then set and save, I will have another record with used_id correctly filled but no values for the rest o the fields.What should I do is either to update existing SnAltceva record, either fill the user_id before it's saved.I suspect that SnUser is saving first the embedded forms and then the main form. And I have the id only after main form saving.
Cristian N
+1  A: 

FOUND IT! (damn, this was tough)

class SnUserForm extends BaseSnUserForm {
    public function configure() {
       // get the SnAltceva object reference (embeded one)
       // if not, create a new one
       $altObj = $this->getObject()->getSnAltceva();
       if( is_null($altObj) ) {
            $altObj = new SnAltceva();
            $this->getObject()->setSnAltceva($altObj);
       }

       // create the form with the already linked obj
       $formAlt = new SnAltcevaForm($altObj);

       $this->embedForm('altceva', $formAlt);
    }
}

No need to override the save() method in SnUser() class. The related table will be automatically saved with the right (fresh) id.

Hope it helps others on the future.

Cristian N