# CakePHP 3: Save deep associated data

**URL:** <https://discourse.cakephp.org/t/cakephp-3-save-deep-associated-data/1080>\
**Category:** Need Help\
**Created:** [August 31, 2016, 4:49pm UTC](https://discourse.cakephp.org/t/cakephp-3-save-deep-associated-data/1080 "2016-08-31T16:49:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salines](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/salines/32/3166_2.png) [@Salines](https://discourse.cakephp.org/u/Salines)\
**Post date:** [August 31, 2016, 4:49pm UTC](https://discourse.cakephp.org/t/cakephp-3-save-deep-associated-data/1080/1 "2016-08-31T16:49:28Z")

</div>

[![enter image description here](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/1X/91f9efab7c8ac5e92ab0f6a6996f853db88b2201.jpg)](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/1X/91f9efab7c8ac5e92ab0f6a6996f853db88b2201.jpg)

This db diagram is part of web application.

On **JobsController** i have **apply($id)** method where user (candidate) must add:

- Applications.cover\_letter
- Applications.attachment (CV)

and many answers (yes / no) at questions related to job:

- Answers.answer

When user submit form app create new records at applications and answers table, but answers table must contain new created applications\_id.

First question:

How to create forms for deep associated tables (answers) at Jobs/apply.ctp ?

```
<?= $this->Form-input('applications.0.cover_letter); ?>

```

but for **many** answers?

```
<?= $this->Form-input('questions.0.answers.answer'); ?>
<?= $this->Form-input('questions.0.answers.question_id); ?>
<?= $this->Form-input('questions.1.answers.answer'); ?>
<?= $this->Form-input('questions.1.answers.question_id); ?>

```

?

---

<div class="post-metadata">

**Author:** ![Salines](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/salines/32/3166_2.png) [@Salines](https://discourse.cakephp.org/u/Salines)\
**Post date:** [September 1, 2016, 8:32am UTC](https://discourse.cakephp.org/t/cakephp-3-save-deep-associated-data/1080/2 "2016-09-01T08:32:09Z")

</div>

**Form:**

```
  <?= $this->Form->create($job); ?>               
    // Job hasMany Applications (Applicants)
    <?= $this->Form->input('applications.0.cover_letter',['type' => 'textarea']); ?>
    <?= $this->Form->hidden('applications.0.job_id',['value' => $job->id]); ?>
        
    <?php foreach ($job->questions as $key => $questions): ?>
       // Job hasMany Question
        <?= $questions->question; ?>

        // Application hasMany Answers
        <?= $this->Form->hidden(sprintf('applications.0.answers.%s.question_id', $key),[
              'value' => $questions->id
              ]); ?>
        <?= $this->Form->radio(sprintf('applications.0.answers.%s.answer', $key),[
              ['value' => 0, 'text' => __('No')],
              ['value' => 1, 'text' => __('Yes')],
              ]); ?><
     <?php endforeach; ?>  

    <?= $this->Form->submit('Apply'); ?>
<?= $this->Form->end(); ?>

```

**JobsController**

```
public function apply($id = null)
{
    $job = $this->Jobs->get($id, [
        'contain' => ['Applications','Questions','Applications.Answers']
    ]);
    if ($this->request->is(['patch', 'post', 'put'])) {
        $job = $this->Jobs->patchEntity($job, $this->request->data,['associated' => ['Applications','Applications.Answers']]);
        if ($this->Jobs->save($job)) {
            $this->Flash->success(__('The Application has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The Application could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('job'));
    $this->set('_serialize', ['job']);
}
```
