title: Cakephp 3.x : Creating a new entity from within another model
body: <p>I'm trying to build a system, where articles can have custom fields. These fields can be text or images. In the latter case, if a picture is uploaded, I would like to create a new Image entity with an entry in the database. The problem is that FieldValues is not associated with Images (since it wont always contain an image).</p>
  
  <p>My plan is to do something like this:</p>
  
  <pre><code>namespace App\Model\Table;
  
  use App\Model\Entity\Fieldvalue;
  use Cake\ORM\Query;
  use Cake\ORM\RulesChecker;
  use Cake\ORM\Table;
  use Cake\Validation\Validator;
  
  class FieldvaluesTable extends Table
  {
  
    ...
  
      function beforeMarshal($event,$data,$options){
          //Check if this is a file upload
          if(is_array($data['data']))
              if(isset($data['data']['name']) &amp;&amp; !empty($data['data']['name'])){
                  $newImage = App\Model\Table\ImagesTable::newEntity();           //This line fails
                  debug($newImage);
                  debug($data['data']);
              }
      }
  }
  </code></pre>
  
  <p>So my question is: Is this a horrible way to go about it? And if not, how do I go about creating a new entity of an unassociated model from within this one?</p>