CakePHP多级模型

时间:2012-07-24 14:10:50

标签: cakephp model associations multi-level

我一直试图弄清楚如何在CakePHP中保存多级模型一段时间,但似乎无法找到解决方案。

我有三个型号。 调查问题选择

  • 调查 hasMany 问题 - 所以问题所属调查 < / p>

  • 问题 hasMany 选择 - 所以选择属于问题 < / p>

    • 问题通过 survey_id 键与调查相关联。

    • 另一方面,选择通过 question_id 键与问题相关联。 (不直接与调查相关联)

表单是通过$this->Form->create('Survey')创建的。

我希望应用程序保存带有相应问题的调查, AND 每个问题都有相应的选择。

问题是,只保存调查问题模型。 选择会被丢弃。

我正在使用$this->saveAssociated($this->request->data, array( 'deep' => true) )

我将更新我的帖子以显示$_POST数据。

谢谢,

XTN

2 个答案:

答案 0 :(得分:0)

您的模型应如下所示:

Survey Model: Survey.php

class Survey extends AppModel {   
    public  $name = 'Survey';   
    public $hasMany = array('Questions' => array(
                                          'className' => 'Question',
                                          'foreignKey' => 'survey_id'
                                                )
                           );
}

Question Model: Question.php

class Question extends AppModel{   
public $name = 'Question';   
public $belongsTo = array(
                        'Survey' => array('className' => 'Survey', 'foreignKey' => 'survey_id'),
                        'Answer' => array('className' => 'Choice', 'foreignKey' => 'answer_id'));
public $hasMany = array('Choices' => array('className' =>  'Choice', 
                                           'foreignKey' => 'question_id'));
}

Choice Model: Choice.php

class Choice extends AppModel
{
public $name = 'Choice';   
public $belongsTo = array('Question' => array('className' => 'Question', 
                                              'foreignKey' => 'question_id'));   
}

请问它是否对您不起作用。

答案 1 :(得分:0)

在保存数据之前,您必须确保数据应采用此格式

在您的控制器中:

$data = array('Survey' => array('id' => 1,'name' => 'test'),
                  'Question' => array(
                                    array('id' => 1,'question' => 'test1','survey_id' => 1,
                                        'Choice' => array(
                                            array('id' => 1,'question_id' => 1,'choice' => 1),
                                            array('id' => 2,'question_id' => 1,'choice' => 2)
                                            )

                                    ),
                                    array('id' => 2,'question' => 'question2','survey_id' => 1,
                                    'Choice' => array(
                                        array('id' => 3,'question_id' => 2,'choice' => 'sd'),
                                        array('id' => 4,'question_id' => 2,'choice' => 'we')
                                        )
                                    )
                                )
                 );
        $this->Survey->create();
        $this->Survey->saveAssociated($data,array('deep'=>true)); 

调查模型:

public $hasMany = array(
    'Question' => array(
        'className' => 'Question',
        'foreignKey' => 'survey_id',
        'dependent' => false,
    )
         );

问题模型:

public $belongsTo = array(
    'Survey' => array(
        'className' => 'Survey',
        'foreignKey' => 'survey_id',
    )
);
public $hasMany = array(
    'Choice' => array(
        'className' => 'Choice',
        'foreignKey' => 'question_id',
        'dependent' => false,
    )
);

选择模型:

public $belongsTo = array(
    'Question' => array(
        'className' => 'Question',
        'foreignKey' => 'question_id',
    )
);

我认为如果发现任何问题会有效,请通知