【问题标题】:saveAll not working with hasMany through (Join Model)saveAll 不能通过(加入模型)与 hasMany 一起使用
【发布时间】:2015-07-03 03:36:38
【问题描述】:

我正在尝试使用最新的 2.x CakePHP 版本进行此操作

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

projects_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 tfg.projects_students (project_id, education_id) VALUES (109, 5)

我认为 CakePHP 正在尝试 2 次插入,使用 (project_id, student_id) 和 (project_id, education_id)

我只想要一个输入,例如 INSERT INTO tfg.projects_students (project_id,student_id,education_id) VALUES (109, 1, 5)

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

---解决方案---

$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));

【问题讨论】:

    标签: cakephp


    【解决方案1】:

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

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

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

    【讨论】:

    • 您不需要连接表中的 id 字段来实现此目的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多