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" <jmbenn...@gmail.com> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to