title: cakephp 3 multi level associated save
body: <p>I have a Users model which has a hasOne relationship to an Employees model. When I save a user I also add a record in the Employees table with the user_id. This works fine.</p>
  
  <p>The Employee can be associated to belongsToMany Courses through CoursesEmployees table. This saves ok when I only save the Employees.</p>
  
  <p>My problem is I want to save all 3 three but the Courses do not get saved.</p>
  
  <p>Save user -> save employee with new user_id -> save courses chosen for new employee with employee_id in courses_employees</p>
  
  <p>EmployeesTable.php</p>
  
  <pre><code>public function initialize(array $config)
  {
      $this-&gt;table('employees');
      $this-&gt;displayField('name');
      $this-&gt;primaryKey('id');
      $this-&gt;addBehavior('Timestamp');
      $this-&gt;belongsTo('Users', [
          'foreignKey' =&gt; 'user_id',
  
      ]);
      $this-&gt;belongsTo('Hotels', [
          'foreignKey' =&gt; 'hotel_id',
          'joinType' =&gt; 'INNER'
      ]);
      $this-&gt;belongsToMany('Courses', [
          'foreignKey' =&gt; 'employee_id',
          'targetForeignKey' =&gt; 'course_id',
          'joinTable' =&gt; 'courses_employees'
      ]);
  }
  </code></pre>
  
  <p>users add.ctp:</p>
  
  <pre><code>&lt;?= $this-&gt;Form-&gt;create($user) ?&gt;
  &lt;fieldset&gt;
      &lt;legend&gt;&lt;?= __('Add User') ?&gt;&lt;/legend&gt;
      &lt;?php
          echo $this-&gt;Form-&gt;input('role_id', ['options' =&gt; $roles, 'empty' =&gt; true]);
          echo $this-&gt;Form-&gt;input('email');
          echo $this-&gt;Form-&gt;input('active');
          echo $this-&gt;Form-&gt;input('activation_key');
          echo $this-&gt;Form-&gt;input('password');
          echo $this-&gt;Form-&gt;input('employee.name');
          echo $this-&gt;Form-&gt;input('employee.email');
          echo $this-&gt;Form-&gt;input('employee.surname');
          echo $this-&gt;Form-&gt;input('employee.employee_num');
          echo $this-&gt;Form-&gt;input('employee.courses._ids', ['options' =&gt; $courses]);
      ?&gt;
  &lt;/fieldset&gt;
  &lt;?= $this-&gt;Form-&gt;button(__('Submit')) ?&gt;
  &lt;?= $this-&gt;Form-&gt;end() ?&gt;
  </code></pre>
  
  <p>Here is the post data sent to the add method for save. With this the user and employee get saved correctly but no recored in the courses_employees.</p>
  
  <p>If I save the employee form the employee controller the courses_employees get add ok so I know the associations are setup ok.</p>
  
  <pre><code>[
  'role_id' =&gt; '1',
  'email' =&gt; 'asdasd@asdasd.com',
  'active' =&gt; '11111111111',
  'activation_key' =&gt; '1',
  'password' =&gt; 'asdasdadasdasdasda',
  'employee' =&gt; [
      'name' =&gt; 'asdasdasdasd',
      'email' =&gt; 'asdasdasd2dasd.com',
      'surname' =&gt; 'asdasdads',
      'employee_num' =&gt; 'asdasdadasdas',
      'courses' =&gt; [
          '_ids' =&gt; [
              (int) 0 =&gt; '2'
          ]
      ]
  ]
  ]
  </code></pre>
  
  <p>UserController.php</p>
  
  <pre><code>public function add()
  {
      $this-&gt;loadModel('Courses');
      $user = $this-&gt;Users-&gt;newEntity();
      if ($this-&gt;request-&gt;is('post')) {
          $user = $this-&gt;Users-&gt;patchEntity($user, $this-&gt;request-&gt;data, [
              'associated' =&gt; ['Employees', 'Courses']
          ]);
          if ($this-&gt;Users-&gt;save($user)) {
              $this-&gt;Flash-&gt;success(__('The user has been saved.'));
              return $this-&gt;redirect(['action' =&gt; 'index']);
          } else {
              $this-&gt;Flash-&gt;error(__('The user could not be saved. Please, try again.'));
          }
      }
      $roles = $this-&gt;Users-&gt;Roles-&gt;find('list', ['limit' =&gt; 200]);
      $courses = $this-&gt;Courses-&gt;find('list', ['limit' =&gt; 200]);
      $this-&gt;set(compact('user', 'roles', 'courses'));
      $this-&gt;set('_serialize', ['user']);
  }
  </code></pre>