我正在尝试保存用户,他们的 Profiles 和一些标签,我的连接表链接配置文件和标签不断搞砸 .
配置文件模型称为“讲师”,标记模型称为“主题” . 教师有一个电话号码和一个邮政编码,由于某种原因,CakePHP认为这些是在我的连接表中创建条目时应该使用的字段 .
My Join table always comes out as:
id | instructor_id | subject_id |
1 | 90210 | 1 | // thinks that the zip code is an instructor_id
2 | 1112223333 | 1 | // thinks that the phone number is an instructor_id
3 | 1 | 1 | // thinks that user_id is an instructor_id
4 | 1 | 1 | // the actual instructor_id, this one's correct
5 | 90210 | 2 |
6 | 1112223333 | 2 |
3 | 1 | 2 |
4 | 1 | 2 |
My Models:
class Instructor extends AppModel
{
var $name = 'Instructor';
var $belongsTo = array('User', 'State');
var $hasAndBelongsToMany = array(
'Subject' => array(
'className' => 'Subject',
'joinTable' => 'instructors_subjects',
'foreignKey' => 'instructor_id',
'associationForeignKey' => 'subject_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
class Subject extends AppModel
{
var $name = 'Subject';
var $hasAndBelongsToMany = array(
'Instructor' => array(
'className' => 'Instructor',
'joinTable' => 'instructors_subjects',
'foreignKey' => 'subject_id',
'associationForeignKey' => 'instructor_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
My Model Associations:
User hasOne Instructor
Instructor belongsTo User
Instructor hasAndBelongsToMany Subject
Subject hasAndBelongsToMany Instructor
My form data looks like:
Array
(
[User] => Array
(
[username] => MrInstructor
[password] => cddb06c93c72f34eb9408610529a34645c29c55d
[group_id] => 2
)
[Instructor] => Array
(
[name] => Jimmy Bob
[email] => jbob@gmail.com
[phone] => 1112223333
[city] => Beverly Hills
[zip_code] => 90210
[states] => 5
[website] => www.jimmybobbaseballschool.com
[description] => Jimmy Bob is an instructor.
[user_id] => 1
[id] => 1
)
[Subject] => Array
(
[name] => hitting, pitching
)
)
My function for processing the form looks like:
function instructor_register()
{
$this->set('groups', $this->User->Group->find('list'));
$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();
echo ' ';
print_r($this->data);
echo ' ';
}
}
}
}
}
3 回答
我从来没有找到为什么CakePHP想要使用我的所有INT字段作为ID,但我能够通过改变一些东西并利用Jamie Fairhurst's tutorial中的解析函数来实现这一点 .
它们不是单独保存每个主题,而是放入一个数组中,然后使用简单的save()与教师同时保存 .
你试过saveAll方法吗?
确保在模型中设置外键 . 因为这是Cake将要使用的,因为你没有列出你的模型,这是我看的第一个地方 .
更多book .
还应该提一下你的连接表不需要一个
id
字段,只需两个外键就可以了 .