CakePHP 4 の bake model 用テンプレ変数まとめ

はじめに

前回と前々回の記事では、コントローラやテンプレートを Bake する際に使用可能な変数をご紹介しました。

今日は bake model 用 Twig テンプレートで使用可能な変数の内容をご紹介します。

動作確認に使用したのは CakePHP 4.2.1 (cakephp/bake 2.1.2) です。

今回は下記マイグレーションで2つのテーブルを作成し、all_dinks テーブルのモデルを Bake したものになります。

/config/Migrations/20210125000001_Init.php
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class Init extends AbstractMigration
{
    public function change()
    {
        // all_dinks table
        $table = $this->table('all_drinks');
        $table->addColumn('drink_type_id', 'integer', [
            'default' => null,
            'limit' => 11,
            'null' => false,
        ]);
        $table->addColumn('name', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => false,
        ]);
        $table->addColumn('created', 'datetime', [
            'default' => null,
            'null' => false,
        ]);
        $table->addColumn('modified', 'datetime', [
            'default' => null,
            'null' => false,
        ]);
        $table->create();

        // drink_types table
        $table = $this->table('drink_types');
        $table->addColumn('name', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => false,
        ]);
        $table->create();
    }
}
$ bin/cake migrations migrate
$ bin/cake bake model AllDrinks
目次
  1. associationInfo
  2. associations
  3. behaviors
  4. connection
  5. displayField
  6. fields
  7. hidden
  8. primaryKey
  9. propertySchema
  10. rulesChecker
  11. table
  12. validation
  13. おわりに

1. associationInfo

array(1) {
  ["DrinkTypes"]=>
  array(1) {
    ["targetFqn"]=>
    string(32) "\App\Model\Table\DrinkTypesTable"
  }
}

2. associations

array(3) {
  ["belongsTo"]=>
  array(1) {
    [0]=>
    array(3) {
      ["alias"]=>
      string(10) "DrinkTypes"
      ["foreignKey"]=>
      string(13) "drink_type_id"
      ["joinType"]=>
      string(5) "INNER"
    }
  }
  ["hasMany"]=>
  array(0) {
  }
  ["belongsToMany"]=>
  array(0) {
  }
}

3. behaviors

特定の条件に一致するカラムがある場合、対応する behavior が入ります。

今回のように created か modified がある場合は Timestamp ビヘイビアが入ります。

array(1) {
  ["Timestamp"]=>
  array(0) {
  }
}

4. connection

string(7) "default"

5. displayField

string(4) "name"

6. fields

array(5) {
  [0]=>
  string(13) "drink_type_id"
  [1]=>
  string(4) "name"
  [2]=>
  string(7) "created"
  [3]=>
  string(8) "modified"
  [4]=>
  string(10) "drink_type"
}

7. hidden

bake コマンド実行時に --hidden オプションを指定した場合は、そのカラムが入ります。

bin/cake bake model AllDrinks --hidden name
array(1) {
  [0]=>
  string(4) "name"
}

未指定の場合は、テーブルに下記カラムがあればそれが入ります。

  • passwd
  • password
  • token

8. primaryKey

array(1) {
  [0]=>
  string(2) "id"
}

9. propertySchema

各カラムの情報が入ります。
(一部省略しています)

array(6) {
  ["id"]=>
  array(3) {
    ["kind"]=>
    string(6) "column"
    ["type"]=>
    string(7) "integer"
    ["null"]=>
    bool(false)
  }
  ...
}

10. rulesChecker

array(1) {
  ["drink_type_id"]=>
  array(2) {
    ["name"]=>
    string(8) "existsIn"
    ["extra"]=>
    string(10) "DrinkTypes"
  }
}

11. table

string(10) "all_drinks"

12. validation

バリデーションに関する情報が入ります。
(一部省略しています)

array(2) {
  ["id"]=>
  array(2) {
    ["integer"]=>
    array(2) {
      ["rule"]=>
      string(7) "integer"
      ["args"]=>
      array(0) {
      }
    }
    ["allowEmpty"]=>
    array(2) {
      ["rule"]=>
      string(16) "allowEmptyString"
      ["args"]=>
      array(2) {
        [0]=>
        string(4) "null"
        [1]=>
        string(8) "'create'"
      }
    }
  }
  ...
}

13. おわりに

今回紹介した変数は Table.twig でも Entity.twig でも使用可能ですが、
中身が一部異なる場合がりますので、必要に応じで dump() 等でご確認ください。