title: CakePHP 3 Saving HasOne association
body: <p><em>possible bug in cakephp framework but not sure about that</em></p>
  
  <h3>I got following MySQL InnoDB tables:</h3>
  
  <pre><code>database.users
      +-----+---------------+----------+
      | id  | user_group_id | username |
      +-----+---------------+----------+
      | INT |           INT | VARCHAR  |
  
  database.user_settings
      +-----+---------------+----------+
      | id  |       user_id |     data |
      +-----+---------------+----------+
      | INT |           INT |  VARCHAR |
  </code></pre>
  
  <h3>I got following intialization in table classes:</h3>
  
  <p><li>Model\Table\UsersTable:</p>
  
  <pre class="lang-php prettyprint-override"><code>$this-&gt;table('users');
  $this-&gt;displayField('id');
  $this-&gt;primaryKey('id');
  $this-&gt;belongsTo('UserGroups', [
      'foreignKey' =&gt; 'user_group_id'
  ]);
  $this-&gt;hasOne('UserSettings', [
      'foreignKey' =&gt; 'user_id'
  ]);
  </code></pre>
  
  <p><li>Model\Table\UserSettingsTable</p>
  
  <pre class="lang-php prettyprint-override"><code>$this-&gt;table('user_settings');
  $this-&gt;displayField('id');
  $this-&gt;primaryKey('id');
  $this-&gt;belongsTo('Users', [
      'foreignKey' =&gt; 'user_id'
  ]);
  
  // And folowing validation rules:
  $validator
      -&gt;add('id', 'valid', ['rule' =&gt; 'numeric'])
      -&gt;allowEmpty('id', 'create')
      // UserSettings.user_id validation rule:
      -&gt;add('user_id', 'valid', ['rule' =&gt; 'numeric'])
      -&gt;requirePresence('user_id', 'create')
      -&gt;notEmpty('user_id');
  </code></pre>
  
  <h3>And I got following code:</h3>
  
  <pre class="lang-php prettyprint-override"><code>$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; ['Users.UserSettings']
      ]);
      // Tried it also this way, won't change anything
      //$user = $this-&gt;Users-&gt;patchEntity($user, $this-&gt;request-&gt;data, [
      //  'associated' =&gt; ['user_setting']
      //]);
      $this-&gt;Users-&gt;save($user,['associated' =&gt; ['UserSettings']]);
  }
  </code></pre>
  
  <p><em>Sample input data ($this->request->data):</em>
  </p>
  
  <pre><code>[
      'user_group_id' =&gt; 1, // Not related to question
      'username' =&gt; 'test', // This will be saved without core modifications
      'user_setting' =&gt; [
          'data' =&gt; 'sample data' // Saved only after "fix" described below
      ]
  ];
  </code></pre>
  
  <p>This will save parent table (users) but not child table (user_settings).</p>
  
  <h3>Then I got following modification I want to get rid of:</h3>
  
  <p>If I put this change to <code>ORM\Associations\HasOne::saveAssociated(...)</code></p>
  
  <pre class="lang-php prettyprint-override"><code>// $this-&gt;property() == 'user_setting'
  if (is_array($targetEntity)) {
      $targetEntity = $this-&gt;target()-&gt;newEntity($targetEntity);
  }
  </code></pre>
  
  <p>It will instantly work the way I wanted to. HasOne actually has required data but it also checks if data is inside Entity, in this case it was in array.</p>
  
  <p>I've played with different association naming combinations and it seems that this is how it should be.</p>
  
  <h3>Main question is how should I save optional hasOne association with parent row?</h3>
  
  <p>Can I somehow make sure that data will be converted into Entity object?
  I think it should just work as there is all required data available and it also seems to handle relations correctly.</p>