Re: getLastInsertId or something similar

2009-01-26 Thread mike

so, I was finaly able to do it like this:

events controller, after save:
$event_id = $this->Event->getLastInsertId();
$this->Session->write('Event.id', $event_id);

users view:
$event_id = $session->read('Event.id');
echo $form->input('Event.id',array('type' => 'hidden', 'value' =>
$event_id ));

users controller:

$user_id = $this->User->getLastInsertId();
$this->User->Event->save($this->data);
$this->data['Event']['creator_id'] = $user_id;

maybe not the best way but at least it's working for now!

On Jan 16, 9:12 pm, mike  wrote:
> I have a multi page input form thingy - on the first page, the user
> inputs some event information, then clicks next, this information is
> saved, then on the next page, the user inputs some user information,
> clicks save, and this information is saved.  The issue is, now the
> event info has to be associated with the user info - i need to set the
> foriegn key to the user in the event table = to the user id just added
>
> so whats the best way to do this?
>
> does using getLastInsertId on the event work in the user controller?
> (doesn't seem like it)
>
> or am I supposed to pass the event id to the user controller through
> the url somehow and get it using params['pass'] or something?
>
> thanks
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-21 Thread Webweave

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  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  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  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"  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 cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-21 Thread Yusuf Widiyatmono
you can only get the value of getLastInsertId() method after saving something...

you have to do this before
$this->User->Event->save($this_data);
$last_event_id = $this->User->Event->getLastInsertId();

and this getLastInsertId() method is only working for save() method to insert 
new record to events table in database.
for using save() method to update a record, the getLastInsertId, will not give 
you anything too


---
is this the answer that you mean? sorry if i am mistaken what your question.



 Best Regards



Yusuf Widiyatmono

Software Engineer
Phone +62 (361) 755 025
Facsimile +62 (361) 755 024
Mobile +62 813 2126 4488
www.mitrais.com






From: mike 
To: CakePHP 
Sent: Thursday, January 22, 2009 9:13:48 AM
Subject: Re: getLastInsertId or something similar


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  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  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"  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 cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-21 Thread mike

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  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  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"  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 cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-21 Thread Webweave

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  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"  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
>
>
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-19 Thread mike

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"  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
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-19 Thread Jon Bennett

> 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

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-18 Thread mike

thanks for all the responses, sorry for being slow, but I don't
understand:

> * grab the 'lastInsertId()' of the event record
> * pass this to the view to use in your User form, eg User.event_id

where do I grab the lastInsertId()?  in the event_controller?  and
then how do I pass it to the user view?

I know there's other ways to do it, but right now I just want to get
what I have working.

Thanks.

On Jan 18, 4:16 am, "Jon Bennett"  wrote:
> >  You can't have an Event.user_id if there's no User. You're doing this 
> > backwards.
>
> yep, so the order of things (doing it event first would be)
>
> * show new event form
> * on submit, save data to create a new Event record
> * grab the 'lastInsertId()' of the event record
> * pass this to the view to use in your User form, eg User.event_id
> * on submit, grab the event id out of the data array, then save the
> user data to create a new form
> * if all ok, grab the 'lastInsertId()' of the user record
> * update the 'user_id' field of the Event you created previously with
> the User id you just created
>
> It's really quite long winded though, I would go for 1 form
> personally, not 2. If you have a lot of fields, you could use some JS
> to show/hide the 2 sections. If you don't want to go the JS route,
> create the User first. This would have the added benefit of working
> for Users who've already registered as well, as you just need to check
> for a logged in user and grab that ID rather than show the 'new user'
> form.
>
> hth
>
> jon
>
> --
>
> jon bennett
> w:http://www.jben.net/
> iChat (AIM): jbendotnet Skype: jon-bennett
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-18 Thread Webweave

You don't have to have two forms, you just have to save things in the
right order.

I do this on one of my volunteer signup (http://volunteercake.sf.net).
Before the user is logged in, I display a list of jobs they can sign
up for, if they click on the 'signup' link, I take them to a page that
does both a user add, and creates the data to signify that new user is
associated with the job slot they chose.

I take the data from the form, insert a new user as normal, grab the
user id with getLastInsertedId, and then do a save for the UserSlot
that creates the association with the new user.

I do this with the following code (note that the _register function is
shared code that is used by the register and signup views):

/**
 * This method handle the user registration process.
 * It will first of all, get the user basic information.
 * After user submit the information, a hash key will be generated
and
 * stored in the database. An email will then send to user and
pending
 * for user activation
 */
function _register()
{
$valid = false;
$this->User->setValidation('register');

if ( !empty( $this->data ) ){
if ( $this->data['User']['user_password']
== $this->Auth->password( $this->data['User']
['user_pass_confirm'])) {

// This function does the creation of the user and
initial setup
// of the ACOs for them.
$this->User->create();
if ( $this->User->save($this->data) ){
$this->data['User']['user_id'] = $this->User-
>getLastInsertId();
$this->data['UserSlot']['user_id'] = $this->data
['User']['user_id'];
$this->save_aro($this->data);
$this->Auth->login($this->data);

// do other stuff here like send activation email
to user
// or/and redirect to other page
$this->Session->setFlash(__('The User has been
saved', true));
$valid = true;
}
} else {
$this->Session->setFlash('password mismatch');
}
}
// Return whether we successfully completed the registration
or not
return $valid;
}


/**
 * Sign up - this method creates a User, and then signs them up
for the
 * slot they picked. Used by links in the page that pass a
slot_id.
 */
function sign_up() {
$this->User->setValidation('register');
if (!empty($this->data)) {
// Try to register (assumes we set up the User data in the
form
if ($this->_register())
{
$this->User->UserSlot->create();

if ($this->User->UserSlot->save($this->data
['UserSlot'])) {
$user_slot_id = $this->User->UserSlot-
>getLastInsertId();
$this->data['UserSlot']['user_slot_id'] =
$user_slot_id;
$slot_id =$this->data['UserSlot']['slot_id'];
$job_id = $this->User->UserSlot->Slot->field
('job_id','Slot.slot_id = '.$slot_id);
$this->Session->setFlash(__('Slot Signup and
Registration complete', true));
$this->redirect('/jobs/view/'.$job_id, null,
true);
} else {
$this->Session->setFlash(__('Signup Signup
incomplete', true));
}
} // Falls through if the registration failed ...
} else {
// No POST data - check if we passed params ...
if ($this->passedArgs['user_id'] > 0)
{
// Try to save the UserSlot for the User ...
if ($this->User->UserSlot->save(array('UserSlot' =>
array( 'slot_id' => $this->passedArgs['slot_id'], 'user_id' => $this-
>passedArgs['user_id'] {
$this->Session->setFlash(__('Signup complete',
true));
} else {
$this->Session->setFlash(__('Signup incomplete',
true));
}
$this->redirect($this->referer(),null, true);
}
// If we got passed a slot_id, then we save it in the
UserSlot array
if (isset($this->passedArgs['slot_id']))
{
// Set the slot to the one we got passed ...
$this->data['UserSlot']['slot_id'] = $this->passedArgs
['slot_id'];
}
}

}


On Jan 18, 1:16 am, "Jon Bennett"  wrote:
> >  You can't have an Event.user_id if there's no User. You're doing this 
> > backwards.
>
> yep, so the order of things (doing it event first would be)
>
> * show new event form
> * on submit, save data to create a new Event record
> * grab the 'lastInsertId()' of the event record
> * pass this to the view to use in your User form, eg User.event_id
> * on submit, grab the event id out of the data array, then save the
> user data to create a new form
> * if all ok, grab the 

Re: getLastInsertId or something similar

2009-01-18 Thread Jon Bennett

>  You can't have an Event.user_id if there's no User. You're doing this 
> backwards.

yep, so the order of things (doing it event first would be)

* show new event form
* on submit, save data to create a new Event record
* grab the 'lastInsertId()' of the event record
* pass this to the view to use in your User form, eg User.event_id
* on submit, grab the event id out of the data array, then save the
user data to create a new form
* if all ok, grab the 'lastInsertId()' of the user record
* update the 'user_id' field of the Event you created previously with
the User id you just created

It's really quite long winded though, I would go for 1 form
personally, not 2. If you have a lot of fields, you could use some JS
to show/hide the 2 sections. If you don't want to go the JS route,
create the User first. This would have the added benefit of working
for Users who've already registered as well, as you just need to check
for a logged in user and grab that ID rather than show the 'new user'
form.

hth

jon

-- 

jon bennett
w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-17 Thread brian

You can't have an Event.user_id if there's no User. You're doing this backwards.

On Sat, Jan 17, 2009 at 5:43 PM, mike  wrote:
>
> The user does not already exist.  The event is being created, then
> immediately after the user is being created.
>
> I forgot to mention, it occured to me to save the user first, then the
> event, but its just for this specific application we decided to do it
> this way.
>
> So how do I set the value of the hidden field Event.user_id to
> User.id?  that is what I'm not understanding.  any examples?
>
> thanks!
>
> On Jan 17, 12:48 am, brian  wrote:
>> Does the User already exist? Or, is the User being created at the same
>> time as the event?
>>
>> Either way, if you create the event with the Event controller, all you
>> need to do is make sure there's a hidden field for Event.user_id and
>> that its value is set to the User.id.
>>
>>
>>
>> On Fri, Jan 16, 2009 at 11:45 PM, mike  wrote:
>>
>> > I'm not sure I understand this.  How would the user controller know
>> > which event its supposed to be related to?
>>
>> > I have the user model set to hasMany Events, and Event belongsTo
>> > User.  In the view I have the hidden field as suggested.
>>
>> > then I tried this:
>> >$this->data['Event']['user_id'] = 
>> > $this->User->getLastInsertId();
>> >$this->User->Event->save($this->data);
>>
>> > but this just adds a new row to the event table, instead of updating
>> > the last inserted one.
>>
>> > On Jan 16, 10:43 pm, "Jon Bennett"  wrote:
>> >> >  However, wouldn't it make more sense to submit the User info before
>> >> >  the Event info? I assume that a User hasMany Event. In which case,
>> >> >  you'd have the User submit their info and ensure that
>> >> >  $this->data['User']['id'] was set before rendering (not redirect to)
>> >> >  the Event form view, which would have:
>>
>> >> >  echo $form->hidden('User.id');
>>
>> >> If this is going in a form tied to the Event model, shouldn't it in fact 
>> >> be:
>>
>> >> // hidden form input for foreign key in events table
>> >> echo $form->hidden('Event.user_id');
>>
>> >> 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 cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-17 Thread mike

The user does not already exist.  The event is being created, then
immediately after the user is being created.

I forgot to mention, it occured to me to save the user first, then the
event, but its just for this specific application we decided to do it
this way.

So how do I set the value of the hidden field Event.user_id to
User.id?  that is what I'm not understanding.  any examples?

thanks!

On Jan 17, 12:48 am, brian  wrote:
> Does the User already exist? Or, is the User being created at the same
> time as the event?
>
> Either way, if you create the event with the Event controller, all you
> need to do is make sure there's a hidden field for Event.user_id and
> that its value is set to the User.id.
>
>
>
> On Fri, Jan 16, 2009 at 11:45 PM, mike  wrote:
>
> > I'm not sure I understand this.  How would the user controller know
> > which event its supposed to be related to?
>
> > I have the user model set to hasMany Events, and Event belongsTo
> > User.  In the view I have the hidden field as suggested.
>
> > then I tried this:
> >                $this->data['Event']['user_id'] = 
> > $this->User->getLastInsertId();
> >                $this->User->Event->save($this->data);
>
> > but this just adds a new row to the event table, instead of updating
> > the last inserted one.
>
> > On Jan 16, 10:43 pm, "Jon Bennett"  wrote:
> >> >  However, wouldn't it make more sense to submit the User info before
> >> >  the Event info? I assume that a User hasMany Event. In which case,
> >> >  you'd have the User submit their info and ensure that
> >> >  $this->data['User']['id'] was set before rendering (not redirect to)
> >> >  the Event form view, which would have:
>
> >> >  echo $form->hidden('User.id');
>
> >> If this is going in a form tied to the Event model, shouldn't it in fact 
> >> be:
>
> >> // hidden form input for foreign key in events table
> >> echo $form->hidden('Event.user_id');
>
> >> 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 cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-17 Thread WebbedIT

If Event belongsTo User then you need a user_id field in the Event
table

If you have a two part form which first creates an Event then creates
a User after submitting the Event part of the form you could then pass
$data['Event']['id'] to the User form (will be available after saving
the Event data) meaning you can run $this->Event->saveField('user_id',
$data['Event']['id']) after saving $this->Event->User (Can all be done
from within the Event controller).

Are there many fields to these two forms though?  Would it not be
better to have it as one form and then use saveAll()?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-16 Thread brian

Does the User already exist? Or, is the User being created at the same
time as the event?

Either way, if you create the event with the Event controller, all you
need to do is make sure there's a hidden field for Event.user_id and
that its value is set to the User.id.

On Fri, Jan 16, 2009 at 11:45 PM, mike  wrote:
>
> I'm not sure I understand this.  How would the user controller know
> which event its supposed to be related to?
>
> I have the user model set to hasMany Events, and Event belongsTo
> User.  In the view I have the hidden field as suggested.
>
> then I tried this:
>$this->data['Event']['user_id'] = 
> $this->User->getLastInsertId();
>$this->User->Event->save($this->data);
>
> but this just adds a new row to the event table, instead of updating
> the last inserted one.
>
> On Jan 16, 10:43 pm, "Jon Bennett"  wrote:
>> >  However, wouldn't it make more sense to submit the User info before
>> >  the Event info? I assume that a User hasMany Event. In which case,
>> >  you'd have the User submit their info and ensure that
>> >  $this->data['User']['id'] was set before rendering (not redirect to)
>> >  the Event form view, which would have:
>>
>> >  echo $form->hidden('User.id');
>>
>> If this is going in a form tied to the Event model, shouldn't it in fact be:
>>
>> // hidden form input for foreign key in events table
>> echo $form->hidden('Event.user_id');
>>
>> hth
>>
>> jon
>>
>> --
>>
>> jon bennett
>> w:http://www.jben.net/
>> iChat (AIM): jbendotnet Skype: jon-bennett
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-16 Thread mike

I'm not sure I understand this.  How would the user controller know
which event its supposed to be related to?

I have the user model set to hasMany Events, and Event belongsTo
User.  In the view I have the hidden field as suggested.

then I tried this:
$this->data['Event']['user_id'] = 
$this->User->getLastInsertId();
$this->User->Event->save($this->data);

but this just adds a new row to the event table, instead of updating
the last inserted one.

On Jan 16, 10:43 pm, "Jon Bennett"  wrote:
> >  However, wouldn't it make more sense to submit the User info before
> >  the Event info? I assume that a User hasMany Event. In which case,
> >  you'd have the User submit their info and ensure that
> >  $this->data['User']['id'] was set before rendering (not redirect to)
> >  the Event form view, which would have:
>
> >  echo $form->hidden('User.id');
>
> If this is going in a form tied to the Event model, shouldn't it in fact be:
>
> // hidden form input for foreign key in events table
> echo $form->hidden('Event.user_id');
>
> hth
>
> jon
>
> --
>
> jon bennett
> w:http://www.jben.net/
> iChat (AIM): jbendotnet Skype: jon-bennett
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-16 Thread brian

Whoops! Yes, what Jon said.

On Fri, Jan 16, 2009 at 10:43 PM, Jon Bennett  wrote:
>
>>  However, wouldn't it make more sense to submit the User info before
>>  the Event info? I assume that a User hasMany Event. In which case,
>>  you'd have the User submit their info and ensure that
>>  $this->data['User']['id'] was set before rendering (not redirect to)
>>  the Event form view, which would have:
>>
>>  echo $form->hidden('User.id');
>
> If this is going in a form tied to the Event model, shouldn't it in fact be:
>
> // hidden form input for foreign key in events table
> echo $form->hidden('Event.user_id');
>
> hth
>
> jon
>
>
> --
>
> jon bennett
> w: http://www.jben.net/
> iChat (AIM): jbendotnet Skype: jon-bennett
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-16 Thread Jon Bennett

>  However, wouldn't it make more sense to submit the User info before
>  the Event info? I assume that a User hasMany Event. In which case,
>  you'd have the User submit their info and ensure that
>  $this->data['User']['id'] was set before rendering (not redirect to)
>  the Event form view, which would have:
>
>  echo $form->hidden('User.id');

If this is going in a form tied to the Event model, shouldn't it in fact be:

// hidden form input for foreign key in events table
echo $form->hidden('Event.user_id');

hth

jon


-- 

jon bennett
w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: getLastInsertId or something similar

2009-01-16 Thread brian

After the Event info has been submitted, if your models are set up
correctly, you should get the Event id included in your User form if
it's in $this->data['Event']['id']

User form:
$form->hidden('Event.id')

However, wouldn't it make more sense to submit the User info before
the Event info? I assume that a User hasMany Event. In which case,
you'd have the User submit their info and ensure that
$this->data['User']['id'] was set before rendering (not redirect to)
the Event form view, which would have:

echo $form->hidden('User.id');

>From your description, I'd think this would be a better way to handle it.

On Fri, Jan 16, 2009 at 9:12 PM, mike  wrote:
>
> I have a multi page input form thingy - on the first page, the user
> inputs some event information, then clicks next, this information is
> saved, then on the next page, the user inputs some user information,
> clicks save, and this information is saved.  The issue is, now the
> event info has to be associated with the user info - i need to set the
> foriegn key to the user in the event table = to the user id just added
>
> so whats the best way to do this?
>
> does using getLastInsertId on the event work in the user controller?
> (doesn't seem like it)
>
> or am I supposed to pass the event id to the user controller through
> the url somehow and get it using params['pass'] or something?
>
> thanks
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



getLastInsertId or something similar

2009-01-16 Thread mike

I have a multi page input form thingy - on the first page, the user
inputs some event information, then clicks next, this information is
saved, then on the next page, the user inputs some user information,
clicks save, and this information is saved.  The issue is, now the
event info has to be associated with the user info - i need to set the
foriegn key to the user in the event table = to the user id just added

so whats the best way to do this?

does using getLastInsertId on the event work in the user controller?
(doesn't seem like it)

or am I supposed to pass the event id to the user controller through
the url somehow and get it using params['pass'] or something?

thanks
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---