saveAll无法使用hasMany通过(加入模型)

时间:2015-06-01 11:52:09

标签: cakephp

我正在尝试使用最后的2.x CakePHP版本

http://i.stack.imgur.com/IylLu.png

如果project_students表中没有education_id,那么表格就完美无缺。但不适用于education_id。

这是我使用education_id的实现:

ProjectsStudent.php

    class ProjectsStudent extends AppModel {

    public $belongsTo = array(
    'Project' => array(
        'className'    => 'Project',
        'foreignKey'   => 'project_id'
    ),
   'Student' => array(
        'className'    => 'Student',
        'foreignKey'   => 'student_id'
    )
);    

Student.php

public $hasMany = array('ProjectsStudent');
    public $hasAndBelongsToMany = array(
        'Education' => array(
            'className' => 'Student',
            'joinTable' => 'projects_students',
            'foreignKey' => 'student_id',
            'associationForeignKey' => 'project_id',
        ),
    );

Project.php

public $belongsTo = 'Status';
    public $hasMany = array(
        'Doc' => array(
            'className' => 'Doc',
            'joinTable' => 'docs',
            'foreignKey' => 'project_id',
        ),
        'History' => array(
            'className' => 'History',
            'joinTable' => 'history',
            'foreignKey' => 'project_id',
        ),
        'ProjectsStudent'
    );
    public $hasAndBelongsToMany = array(
        'Student' => array(
            'className' => 'Student',
            'joinTable' => 'projects_students',
            'foreignKey' => 'project_id',
            'associationForeignKey' => 'student_id',
        ),
        'Professor' => array(
            'className' => 'Professor',
            'joinTable' => 'projects_professors',
            'foreignKey' => 'project_id',
            'associationForeignKey' => 'professor_id',
        ),
        'Education' => array(
            'className' => 'Education',
            'joinTable' => 'projects_educations',
            'foreignKey' => 'project_id',
            'associationForeignKey' => 'education_id',
        ),
    );

ProjectsController.php

$data = array(
                'Project' => array(
                    'id' => $id, 
                    'status_id' => 5,
                    'Student' => array($this->Auth->user('id'))),
                'History' => array(
                    array(
                        'action' => 'update -> status 5',
                        'student_id' => $this->Auth->user('id'),
                    )
                ),
                'ProjectsStudent' => array(array(
                    'education_id' => "5")) 
            );
            $this->Project->saveAll($data, array('deep' => true))

输出错误:

错误:SQLSTATE [23000]:完整性约束违规:1062密钥'PRIMARY'的重复条目'109'

SQL查询:INSERT INTO tfgprojects_studentsproject_ideducation_id)VALUES(109,5)

我认为CakePHP正在尝试2次插入,包含(project_idstudent_id)和(project_ideducation_id

我想要一个输入,例如INSERT INTO tfgprojects_studentsproject_idstudent_ideducation_id)VALUES(109,1,5)< / p>

提前致谢,对不起我的英语。

--- ---解

$data = array(
            'Project' => array(
                'id' => $id, 
                'status_id' => 5,
                'Student' => array($this->Auth->user('id'))),
            'History' => array(
                array(
                    'action' => 'update -> status 5',
                    'student_id' => $this->Auth->user('id'),
                )
            ),

        );

        $this->Project->saveAll($data, array('deep' => true));

        $this->Project->ProjectsStudent->updateAll(
                array('education_id' => $this->Auth->user('education')),
            array('project_id' => $this->Project->id));

1 个答案:

答案 0 :(得分:1)

您似乎已设置projects_students表以使用project_id作为主键。主键必须是唯一的,而project_id外键可能是重复的(似乎正在发生在您身上)。连接表至少应该是: -

id (int)
project_id (int)
student_id (int)

其中id是主键(启用了自动增量)。