You would only need to use the getLastInsertId() if you had inserted something and then immediately needed to add related data.
If your forms walk you through some process which goes to another controller, you'd either need to pass the data along, or fetch it again. Like I said before, I have a similar thing where I create a new user and register them for a slot. So, what I do is have an action in my User controller that first creates the User, then does the getLastInsertId, then adds the related row. The getLastInsertId call comes after the save for each data bit and is used in the foreign key field for the related data before that is saved. So, lets say you want to add a new user and a few events. You would do the following: 1. Insert the user data using a $this->User->save($this->data) 2. Save the user ID into a field by calling $this->User- >getLastInsertId() $user_id = $this->User->getLastInsertId(); 3. Loop through the event data and update the foreign key field with the saved user ID $this->data['Event']['user_id'] = $user_id; 4. Save the event data ($this->User->Event->save($this->data ['Event'];) 5. drop through to your next view as normal depending on success or failure of the above. On Jan 21, 4:13 pm, mike <[email protected]> wrote: > does this mean I can't use getLastInsertId() if I inserted the event > in another controller? (this was one of my original questions) > > In the events controller, I save the event, then redirect to the user > view. In the user controller is where I'm trying to get the event_id > I just inserted. > > On Jan 21, 4:49 pm, Webweave <[email protected]> wrote: > > > It will only return a value after you do an insert (a save of the new > > Event). > > > Post the action you are having trouble with, and perhaps we can spot > > the issue. > > > On Jan 19, 3:41 pm, mike <[email protected]> wrote: > > > > eeerrr, this is not working. > > > > I have this in the User model: > > > var $hasMany = array ('Event'); > > > > this in the event model: > > > var $belongsTo = array ( > > > 'User' => array( > > > 'className' => 'User', > > > 'foreignKey' => 'creator_id', > > > ) > > > ); > > > > $this->User->Event->getLastInsertId() in the users controller returns > > > nothing! > > > > what am I missing?? > > > > On Jan 19, 3:01 am, "Jon Bennett" <[email protected]> wrote: > > > > > > where do I grab the lastInsertId()? in the event_controller? and > > > > > then how do I pass it to the user view? > > > > > provided you've correctly set up your model associations, which I think > > > > are: > > > > > User hasMany Event > > > > Event belongsTo User > > > > > From your Users controller you can do: > > > > > $event_id = $this->User->Event->getLastInsertId(); > > > > > hth > > > > > jon > > > > > -- > > > > > jon bennett > > > > w:http://www.jben.net/ > > > > iChat (AIM): jbendotnet Skype: jon-bennett- Hide quoted text - > > > - Show quoted text - > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
