CakePHPではテーブルのカラム名をいい感じに指定すると、外部キーを指定しなくても勝手にリレーションしてくれます。
Formのname指定をいい感じに指定してあげると、簡単なsaveメソッドで、関連するテーブルにも勝手にデータを保存してくれます。
$this->Form->create($article);
// Article 入力
echo $this->Form->control('title');
// Author 入力 (belongsTo)
echo $this->Form->control('author.id');
echo $this->Form->control('author.first_name');
echo $this->Form->control('author.last_name');
// Author の profile (belongsTo + hasOne)
echo $this->Form->control('author.profile.id');
echo $this->Form->control('author.profile.username');
// Tags 入力 (belongsToMany)
echo $this->Form->control('tags.0.id');
echo $this->Form->control('tags.0.name');
echo $this->Form->control('tags.1.id');
echo $this->Form->control('tags.1.name');
// belongsToMany の複数選択要素
echo $this->Form->control('tags._ids', [
'type' => 'select',
'multiple' => true,
'options' => $tagList,
]);
// 結合テーブルの入力 (articles_tags)
echo $this->Form->control('tags.0._joinData.starred');
echo $this->Form->control('tags.1._joinData.starred');
// Comments 入力 (hasMany)
echo $this->Form->control('comments.0.id');
echo $this->Form->control('comments.0.comment');
echo $this->Form->control('comments.1.id');
echo $this->Form->control('comments.1.comment');
https://book.cakephp.org/3.0/ja/views/helpers/form.html#associated-form-inputs
$this->request->getData()が以下のような感じの形になれば保存されるようになります。
$data = [
'first_name' => 'Sally',
'last_name' => 'Parker',
'courses' => [
[
'id' => 10,
'_joinData' => [
'grade' => 80.12,
'days_attended' => 30
]
],
// 他のコース
]
];
$student = $this->Students->newEntity($data, [
'associated' => ['Courses._joinData']
]);
https://book.cakephp.org/3.0/ja/orm/saving-data.html#id20


