2

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

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

  • 调查有很多问题-所以问题属于调查

  • 问题有很多选择-所以选择属于问题

    • 问题通过其survey_id链接到调查。

    • 另一方面,Choice是通过其question_id键链接到Question的。(虽然没有直接链接到调查

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

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

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

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

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

谢谢,

XTN

4

2 回答 2

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'));   
}

请询问它是否不适合您。

于 2012-07-25T05:19:18.890 回答
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',
    )
);

如果发现任何问题,我认为它会起作用,请通知

于 2012-07-25T06:38:45.643 回答