使用2个HABTM和一个hasMany关联保存数据

时间:2014-08-04 10:36:10

标签: cakephp cakephp-2.3 has-and-belongs-to-many

我正在尝试使用多个HABTM& hasMany关系。即使我使用

$this->Project->save();

$this->Project->saveMany();

$this->Project->saveAssociated();

$this->Project->saveAlll();

它没有保存相关模型,我无法弄清楚我做错了什么。

模型版本

  1. 项目hasAndBelongsToMany技能
  2. 项目hasMany里程碑
  3. 项目hasAndBelongsToMany附件(因为Milestone也会有附件,因此无法绑定hasMany关系中的项目和附件)
  4. 项目模型

    class Project extends AppModel{
    
        public $belongsTo = array('Profile');
    
        public $hasMany = array('Milestone');
    
        public $hasAndBelongsToMany = array(
            'Skill' =>
            array(
                'className' => 'Skill',
                'joinTable' => 'projects_skills',
                'foreignKey' => 'project_id',
                'associationForeignKey' => 'skill_id',
                'dependent' => true,
                //'conditions' => array('Advertisement.advertisement_is_active' => '1')
            ),
            'Attachment' =>
            array(
                'className' => 'Attachment',
                'joinTable' => 'attachments_projects',
                'foreignKey' => 'project_id',
                'associationForeignKey' => 'attachment_id',
            )
        );
    
        public function beforeSave($options = array()) {
            parent::beforeSave($options = array());
                foreach (array_keys($this->hasAndBelongsToMany) as $model):
                    if(isset($this->data[$this->alias][$model])):
                        $this->data[$model][$model] = $this->data[$this->alias][$model];
                        unset($this->data[$this->alias][$model]);
                    endif;
                endforeach;
            return true;
        }
    }  
    

    技能型号

    class Skill extends AppModel {
    
        public $name = 'Skill';
        public $hasOne = array('Exam');
        public $hasAndBelongsToMany = array(
            'Category' =>
            array(
                'className' => 'Category',
                'joinTable' => 'categories_skills',
                'foreignKey' => 'skill_id',
                'associationForeignKey' => 'category_id',
                'dependent' => true
            ),
            'Profile' =>
            array(
                'className' => 'Profile',
                'joinTable' => 'profiles_skills',
                'foreignKey' => 'skill_id',
                'associationForeignKey' => 'profile_id',
                'dependent' => true
            ),
            'Project' =>
            array(
                'className' => 'Project',
                'joinTable' => 'projects_skills',
                'foreignKey' => 'skill_id',
                'associationForeignKey' => 'project_id',
                //'dependent' => true
            )
        );
    
    }
    

    附件模型

    class Attachment extends AppModel{
    
        public $name = 'Attachment';
    
        public $hasAndBelongsToMany = array(
            'Project' =>
            array(
                'className' => 'Project',
                'joinTable' => 'attachments_projects',
                'foreignKey' => 'attachment_id',
                'associationForeignKey' => 'project_id',
                'dependent' => true,
            )
        );
    
    }
    

    数据

    Array
    (
        [Project] => Array
            (
                [project_name] => Name Of Project
                [project_detail] => Dummy short description
                [project_category_id] => 3
                [project_budget] => 45
                [Skill] => Array
                    (
                        [0] => Array
                            (
                                [skill_id] => 14
                            )
    
                        [1] => Array
                            (
                                [skill_id] => 16
                            )
    
                    )
    
                [project_status] => 1
                [project_created] => 2014-08-04 15:31:00
                [creator_id] => 1
            )
    
        [Milestone] => Array
            (
                [milestone_name] => Milestone 1
                [milestone_budget] => 45
                [milestone_created_on] => 2014-08-04 15:31:00
                [project_id] => 8
            )
    
        [Attachment] => Array
            (
                [attachment_path] => 11615297103.zip
                [attachment_term] => Project Attachment
                [attachment_type] => zip
                [project_id] => 8
            )
    
    )
    

1 个答案:

答案 0 :(得分:3)

传递技能id而不是关系模型的字段名称

[Skill] => Array
                (
                    [0] => Array
                        (
                            [id] => 14
                        )

                    [1] => Array
                        (
                            [id] => 16
                        )

                )