Search code examples
cakephpmodelassociationsmulti-level

CakePHP multilevel models


I've been trying to figure out how to save multi-level models in CakePHP for some time now, but can't seem to find a solution.

I have three models. Survey, Question, and Choice.

  • Survey hasMany Question - so Question belongsTo Survey

  • Question hasMany Choice - so Choice belongsTo Question

    • The Question is linked to the Survey through its survey_id key.

    • The Choice, on the otherhand, is linked to the Question through its question_id key. (not directly linked to Survey though)

The form is created through $this->Form->create('Survey').

I want the application to save a survey with its corresponding questions, AND each question having their corresponding choice(s).

The problem is, only the Survey and Question models are saved. Choice gets discarded.

I'm using $this->saveAssociated($this->request->data, array( 'deep' => true) )

I will be updating my post to show the $_POST data.

Thanks,

XTN


Solution

  • Before saving data, you have to ensure that data should be in this format

    in your controller:

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

    Survey Model :

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

    Question Model :

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

    Choice Model :

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

    I think it will work if found any problem please notify