Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP是否将所有INT字段视为ID';连接表是什么?_Cakephp_Has And Belongs To Many - Fatal编程技术网

CakePHP是否将所有INT字段视为ID';连接表是什么?

CakePHP是否将所有INT字段视为ID';连接表是什么?,cakephp,has-and-belongs-to-many,Cakephp,Has And Belongs To Many,我正试图保存一个用户、他们的个人资料和一些标记,而我的连接表链接的个人资料和标记不断地变得混乱 配置文件模型称为讲师,标签模型称为主题。讲师有电话号码和邮政编码,出于某种原因,CakePHP认为在我的联接表中创建条目时应该使用这些字段 我的联接表总是显示为: id | instructor_id | subject_id | 1 | 90210 | 1 | // thinks that the zip code is an instructor_id 2 |

我正试图保存一个用户、他们的个人资料和一些标记,而我的连接表链接的个人资料和标记不断地变得混乱

配置文件模型称为讲师,标签模型称为主题。讲师有电话号码和邮政编码,出于某种原因,CakePHP认为在我的联接表中创建条目时应该使用这些字段

我的联接表总是显示为:

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 | 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' => '' ) ); } User hasOne Instructor Instructor belongsTo User Instructor hasAndBelongsToMany Subject Subject hasAndBelongsToMany Instructor 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 ) ) 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 '
id |讲师|科目| id|
1 | 90210 | 1 |//认为邮政编码是讲师id
2 | 111222333 | 1 |//认为电话号码是讲师id
3 | 1 | 1 |//认为用户id是讲师id
4 | 1 | 1 |//实际的讲师id,这个是正确的
5  | 90210         | 2          |
6  | 1112223333    | 2          |
3  | 1             | 2          |
4  | 1             | 2          |
我的型号:

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 | 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' => '' ) ); } User hasOne Instructor Instructor belongsTo User Instructor hasAndBelongsToMany Subject Subject hasAndBelongsToMany Instructor 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 ) ) 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 '
课堂教师扩展应用程序模型
{
var$name=‘讲师’;
var$belongsTo=array('User','State');
变量$hasAndBelongsToMany=数组(
“主题”=>数组(
'className'=>'Subject',
“joinTable”=>“讲师/科目”,
“外键”=>“讲师id”,
'associationForeignKey'=>'subject\u id',
“唯一”=>正确,
'条件'=>'',
'字段'=>'',
“订单”=>“”,
'限制'=>'',
'偏移量'=>'',
“finderQuery'=>”,
'删除查询'=>'',
'insertQuery'=>'
)   
);
}
类主题扩展了AppModel
{
var$name=‘主体’;
变量$hasAndBelongsToMany=数组(
“讲师”=>数组(
“className”=>“讲师”,
“joinTable”=>“讲师/科目”,
'foreignKey'=>'subject\u id',
“associationForeignKey”=>“讲师id”,
“唯一”=>正确,
'条件'=>'',
'字段'=>'',
“订单”=>“”,
'限制'=>'',
'偏移量'=>'',
“finderQuery'=>”,
'删除查询'=>'',
'insertQuery'=>'
)   
);
}
我的模型关联:

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 | 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' => '' ) ); } User hasOne Instructor Instructor belongsTo User Instructor hasAndBelongsToMany Subject Subject hasAndBelongsToMany Instructor 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 ) ) 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 '
用户只有一名讲师
讲师属于用户
讲师有许多科目
受试者有一名助教
我的表单数据如下所示:

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 | 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' => '' ) ); } User hasOne Instructor Instructor belongsTo User Instructor hasAndBelongsToMany Subject Subject hasAndBelongsToMany Instructor 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 ) ) 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 '
排列
(
[用户]=>阵列
(
[用户名]=>MR讲师
[密码]=>cddb06c93c72f34eb9408610529a34645c29c55d
[组id]=>2
)
[讲师]=>阵列
(
[姓名]=>吉米·鲍勃
[电子邮件]=>jbob@gmail.com
[电话]=>111222333
[城市]=>贝弗利山庄
[邮政编码]=>90210
[国家]=>5
[网站]=>www.jimmybobbaseballschool.com
[说明]=>吉米·鲍勃是一名讲师。
[用户id]=>1
[id]=>1
)
[主题]=>数组
(
[名称]=>击球、投球
)
)
我处理表单的函数如下所示:

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 | 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' => '' ) ); } User hasOne Instructor Instructor belongsTo User Instructor hasAndBelongsToMany Subject Subject hasAndBelongsToMany Instructor 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 ) ) 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 '
函数寄存器()
{
$this->set('groups',$this->User->Group->find('list');
$this->set('states',$this->User->讲师->State->find('list');
如果(!empty($this->data)){
//将小组设置为讲师
$this->data['User']['group_id']=2;
//保存用户数据
$user=$this->user->save($this->data,true,数组)(
“用户名”,
“密码”,
“组id”
));
//如果用户已保存,请保存讲师的信息
如果(!empty($user)){
$this->data['讲师]['user_id']=$this->user->id;
$讲师=$this->User->讲师->保存($this->data,true,数组)(
“用户id”,
“姓名”,
“电子邮件”,
"电话",,
"城市",,
“邮政编码”,
“州id”,
"网站",,
“说明”
));
//如果讲师已获救,则保存其余讲师
如果(!空($讲师)){
$讲师id=$this->User->讲师->id;
$this->data['讲师]['id']=$instructorId;
//分别保存每个主题
$Subject=explode(“,”,$this->data['Subject']['name']);
foreach($主题作为$主题){
//获取正确的主题格式
$\受试者=strtolower(修剪($\受试者));
$this->User->讲师->主题->创建($this->data);
$this->User->讲师->Subject->set(数组)(
“名称”=>$\u主题
));
$this->User->讲师->主题->保存();
回声';
<?php

class Profile extends AppModel {
    var $name = 'Profile';                
    var $belongsTo = array(
        'User' => array(
            'className'    => 'User',
            'foreignKey'    => 'user_id'
        )
    );  
}
?>
打印($this->data); 回声';
<?php

class Profile extends AppModel {
    var $name = 'Profile';                
    var $belongsTo = array(
        'User' => array(
            'className'    => 'User',
            'foreignKey'    => 'user_id'
        )
    );  
}
?>
} } } } }
确保在模型中设置外键。因为这是蛋糕将要使用的,因为你还没有列出你的模型,所以这是我首先要看的地方

if(!empty($this->data)) {
  $this->User->saveAll($this->data, array('validate'=>'first'));
}

更多信息请访问

还应该提到你的加入