CakePHP3:bakeの落とし穴

とても便利なbakeですが、hasOneのリレーションを設定してくれないという特徴があります。

テーブルの例

例えば、次のようなテーブルがあるとします: usersテーブル

f:id:thebaker:20181021001722p:plain

user_profilesテーブル

f:id:thebaker:20181021000750p:plain

bakeした結果

Users hasMany UserProfilesというモデルが生成されます。

UsersTable.php

    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('users');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->hasMany('UserProfiles', [
            'foreignKey' => 'user_id'
        ]);
    }

UserProfilesTable.php

    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('user_profiles');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);
    }

なお、この挙動はuser_profiles.user_idカラムにユニークキーを貼っても変わりありません。

このままhasManyにしたまま進むとどうなるか?

bakeへの影響

コントローラやテンプレートをbakeした際に、hasMany前提でbakeされてしまいます。

その他の影響

フォームで関連するデータの保存を行う場合に、添字の指定が必要になります:

$this->Form->control('email');
$this->Form->control('user_profile.0.first_name'); // hasOneでbakeしていればuser_profile.first_nameと書ける
$this->Form->control('user_profile.0.last_name');

bake allは使わないほうが良さそう

bake allをすると、hasOneにしたいのにhasManyになり、hasMany前提でコントローラとテンプレートが生成されてしまいます。 よって、次のような手順が良さそうです:

  • まず、モデルをbakeして適宜hasManyをhasOneに修正する
  • コントローラ、テンプレートをbakeする