I'm putting together a database of instructors and I'm having a little
trouble with saving my associated models.
My tables are 'users', 'instructors', 'subjects', and
'instructors_subjects'. My model User hasOne Instructor. My model
Instructor hasAndBelongsToMany Subjects.
My function looks like this:
function instructor_register()
{
$this->set('states', $this->User->Instructor->State-
>find('list'));
if (!empty($this->data)) {
// Set the group to Instructor
$this->data['User']['group_id'] = 2;
// Save the user data
$user = $this->User->save($this->data, true, array(
'username',
'password',
'group_id'
));
// If the user was saved, save the instructor's info
if (!empty($user)) {
$this->data['Instructor']['user_id'] = $this->User-
>id;
$instructor = $this->User->Instructor->save($this-
>data, true, array(
'user_id',
'name',
'email',
'phone',
'city',
'zip_code',
'state_id',
'website',
'description'
));
// If the instructor was saved, save the rest
if(!empty($instructor)) {
$instructorId = $this->User->Instructor->id;
$this->data['Instructor']['id'] = $instructorId;
// Save each subject seperately
$subjects = explode(",", $this->data['Subject']
['name']);
foreach ($subjects as $_subject) {
// Get the correct subject format
$_subject = strtolower(trim($_subject));
$this->User->Instructor->Subject->create($this-
>data);
$this->User->Instructor->Subject->set(array(
'name' => $_subject
));
$this->User->Instructor->Subject->save();
}
}
}
}
}
My data looks like this:
Array
(
[User] => Array
(
[username] => MrInstructor
[password] => cddb06c93c72f34eb9408610529a34645c29c55d
[group_id] => 2
)
[Instructor] => Array
(
[name] => Jimmy Bob
[email] => [email protected]
[phone] => 1112223333
[city] => Beverly Hills
[zip_code] => 90210
[states] => 5
[website] => www.jimmybobschool.com
[description] => Jimmy Box is an instructor.
[user_id] => 1
[id] => 1
)
[Subject] => Array
(
[name] => hitting, pitching
)
)
Array
(
[0] => hitting
[1] => pitching
)
For some reason my join table for Instructors and Subjects
(instructors_subjects) has a bunch of extra entries. There's an entry
for both hitting and pitching that is correct with the proper
instructor_id and the subject_id. But, then there's a bunch of entries
where the instructor_id is either the phone number, the zip code, or
the user id.
Anyone know why these other fields are getting confused as
instructor_id's?
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
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