Cake 3.0 Triple Association

Hello,

I have three tables:
Questionnaires (has many Questions),
Questions (has many Answers),
Answers

I added QuestionnairesTable.php and QuestionsTable.php and it have hasMany definition in initialize function:
QuestionnairesTable :
$this->hasMany(‘Questions’)
->setForeignKey(‘questionnaire_id’)
->setDependent(true);

QuestionsTable :
$this->hasMany(‘Answers’)
->setForeignKey(‘question_id’)
->setDependent(true);

I am adding a Questionnaire, its Questions and Answers using one add.ctp and want to save it using
$this->Questionnaires ->patchEntity($item, $this->request->data);
which is in add function in QuestionnairesController.

data array looks like this:
Array
(
[name] => New Questionnaire
[questions] => Array
(
[1] => Array
(
[text] => Question text
[answers] => Array
(
[1] => Array
(
[correct] => 1
[text] => Answer text
)
)
)
)
)

When I try to save, it save Questionnaire and Question but do not save Answers. What should i do to save Answers ?

Martin

Nevermind. I solve it.

Added associated to patchEntity function like this:
$item = $this-> Questionnaires->patchEntity($item, $this->request->data, [
‘associated’ => [‘Questions’, ‘Questions.Answers’]
]);

1 Like