使用模板烘焙代码的工具


README

需求

infotechnohelp/cakephp-orm

requireAtLeastOne, requireOnlyOne, requireAtLeastOneOfGroups, requireOnlyOneOfGroups

infotechnohelp/cakephp-seeds

跟踪的种子

composer require infotechnohelp/bakery

基本 CakePHP 模型-模式使用

基本数据库配置

BookGenres:
  title:str:
  subtitle:str:
  parent_id:int: {_null: true}

如果没有指定,则使用默认字段类型配置,模型-模式字段配置项会覆盖默认配置

由于 Yaml 解析限制,使用 _null 代替 null

unique 不是一个字段配置,但在迁移中添加了唯一索引(+ TableClass 验证)

时间戳行为

默认实现了时间戳行为

Books:
  name:s: {unique: true}
  timestampBehaviour: false

链接字段

单一

BookGenres.Books:
  name:s: {unique: true}
  timestampBehaviour: false

添加 book_genre_id 字段(+ TableClass 和 EntityClass 中的所有额外字段的关系)

BooksTable 类中添加 belongsTo BookGenres 关系

BookGenresTable 类中添加 hasMany Books 关系

多个

BookTypes+BookGenres.Books:
  name:s: {unique: true}
  timestampBehaviour: false

可空

BookGenres{null}.Books:
  name:s: {unique: true}
  timestampBehaviour: false

字段 book_genre_id 可以留空(null)

插件前缀

Infotechnohelp/Authentication:Users.Books:
  name:s: {unique: true}
  timestampBehaviour: false

使用 Infotechnohelp/Authentication 插件前缀

@todo

检查插件前缀是否可以与链接表一起使用(必须)

自定义外键

Guests(friend_id).Books:
  name:s: {unique: true}
  timestampBehaviour: false

创建并使用 friend_id 字段代替 guest_id

自定义外键和属性名

Guests(friend_id,friend).Books:
  name:s: {unique: true}
  timestampBehaviour: false

创建并使用 friend_id 字段代替 guest_id,使用 friend 属性名代替 guest

自定义属性名

Guests(,friend).Books:
  name:s: {unique: true}
  timestampBehaviour: false

使用 friend 属性名代替 guest

链接表

Authors..Books:
  name:s: {unique: true}
  timestampBehaviour: false

创建 AuthorsBooks 表(根据 CakePHP 规范命名)

BooksTable 类中添加 belongsToMany Authors 关系

AuthorsTable 类中添加 belongsToMany Books 关系

AuthorsBooksTable 类中添加 belongsTo BooksbelongsTo Authors 关系

链接字段和表的组合

首先指定链接表,然后指定链接字段

Authors+Translators..BookGenres+BookTypes.Books:
  name:s: {unique: true}
  timestampBehaviour: false

额外的数据库配置

db_BookGenres:
  uniqueFieldGroups: [[title, subtitle]]

注意!!! 在默认数据库配置之后仅提供以 db_database_ 前缀配置的配置

在迁移文件中添加了组合唯一索引

->addIndex(['title', 'subtitle', ], ['unique' => true])

在 TableClass 中添加了 isUnique 规则

$rules->add($rules->isUnique(
['title', 'subtitle', ],
"This ['title', 'subtitle', ] combination has already been used."
));

uniqueFieldGroups 可以使用单个字段组

db_Prices:
  uniqueFieldGroups: [[book_genre_id]]

在迁移文件中添加了单个字段唯一索引

->addIndex(['book_genre_id', ], ['unique' => true])

在 TableClass 中添加了 isUnique 规则

$rules->add($rules->isUnique(['book_genre_id']));

表类配置

t_BookGenres:
  rewrite-relations:
    hasOne: [Profiles]
  add-relations:
    belongsTo:
      ParentCategories: {className: BookGenres, foreignKey: parent_id}
    hasMany:
      ChildCategories: {className: BookGenres, foreignKey: parent_id}
    hasOne:
      AnyGuests: {className: Guests, foreignKey: admin_id, property: any_guest, conditions: "['AnyGuests.surname' => 'Any']"}
  validations:
    first_names: {minLength:[5]}
    second_names: {minLength:[12]}
  rules:
    requireAtLeastOneOf: [user_id, friend_id]

t_table_ 前缀

重写关系

t_BookGenres:
  rewrite-relations:
    hasOne: [Books]

默认情况下,用于链接字段的关系类型是 hasMany

如果需要将其更改为 hasOne,请使用上面的示例

添加关系

t_BookGenres:
  add-relations:
    belongsTo:
      ParentCategories: {className: BookGenres, foreignKey: parent_id}
    hasMany:
      ChildCategories: {className: BookGenres, foreignKey: parent_id}

关系配置可以包括自定义条件

t_Admins:
  add-relations:
    hasOne:
      AnyGuests: {className: Guests, foreignKey: admin_id, property: any_guest, conditions: "['AnyGuests.surname' => 'Any']"}

在 EntityClass 中添加 any_guest 字段

验证

t_Profiles:
  validations:
    first_names: {minLength:[5]}
    second_names: {minLength:[12]

用于 first_namessecond_names 字段的验证

可能的验证

minLength:[5]
maxLength:[7]
isJson: null

@todo 检查是否可以手动添加以下验证

greaterThanOrEqual
scalar
numeric
integer
isUnique
requirePresence
allowEmptyString

规则

t_Profiles:
  rules:
    requireAtLeastOneOf: [user_id, friend_id]

可能的规则

requireAtLeastOneOf: [user_id, friend_id]
requireAtLeastOneOfGroups: [[first_names, second_names], [company_name]]
requireOnlyOneOf: [user_id, guest_id]
requireOnlyOneOfGroups: [[first_names, second_names], [company_name]]

这些规则来自 infotechnohelp/cakephp-orm

实体类配置

entity_Guests:
  data:
    constantContainer:
    seedField: title
    seeds: [Guest1, Guest2]
  virtual:
    profile_second_name: [profile, second_name]

e_entity_ 前缀

数据

entity_Guests:
  data:
    constantContainer:
    seedField: title
    seeds: [Guest1, Guest2]

constantContainer 配置自动在 EntityClass 中创建常量

const GUEST1 = 1;
const GUEST2 = 2;

Guests 添加到 config/TrackedSeeds/_Queue.php 列表

创建 config/TrackedSeeds/Guests.php

<?php declare(strict_types = 1);

$tableAlias = "Guests";

$data = [
[
'title' => 'Guest1',
],
[
'title' => 'Guest2',
],
];
e_Admins:
  data:
    constantContainer:
    constantField: name
    seeds: [{name: Admin1, pwd: pwd1}, {name: Admin2, pwd: pwd2}]

constantContainer 配置自动在 EntityClass 中创建常量

const ADMIN1 = 1;
const ADMIN2 = 2;

Admins 添加到 config/TrackedSeeds/_Queue.php 列表

创建 config/TrackedSeeds/Admins.php

<?php declare(strict_types = 1);

$tableAlias = "Admins";

$data = [
[
'name' => 'Admin1',
'pwd' => 'pwd1',
],
[
'name' => 'Admin2',
'pwd' => 'pwd2',
],
];

虚拟字段

entity_Guests:
  virtual:
    profile_second_name: [profile, second_name]

向 EntityClass 添加虚拟字段

protected $_virtual = [
'profile_second_name',
];

protected function _getProfileSecondName() {
/** @var Entity $Profile */
$Profile = $this->_properties['profile'];
return (!empty($Profile)) ? $Profile->get('second_name') : false;
}

FileSpecificTemplates

config/Bakery/FileSpecificTemplates

.../config/TrackedSeeds/_Queue/start.txt

.../src/Model/Entity/Guest
   use.txt
   accessible.txt
   classBody.txt
   
.../src/Model/Table/Guests 
   use.txt
   initialize.txt
   defaultValidations.txt
   buildRules.txt
   classBody.txt

面包店设置

结构

config/Bakery/settings/...

../setting/main.yml

默认值

needle: '#'
markers: false

../setting/Migrations/columnTypeMap.yml

默认值

s: string
str: string
te: text
text: text
i: integer
int: integer
float: float
f: float
fl: float
b: boolean
bo: boolean
bool: boolean
datetime: datetime
dt: datetime
d: date
t: time

../setting/Migrations/columnTypeDefaults/boolean.yml

默认值

default: null
_null: false

../setting/Migrations/columnTypeDefaults/datetime.yml

默认值

default: null
_null: false

../setting/Migrations/columnTypeDefaults/float.yml

默认值

default: null
limit: 11
_null: false
signed: false

../setting/Migrations/columnTypeDefaults/integer.yml

默认值

default: null
limit: 11
_null: false
signed: false

../setting/Migrations/columnTypeDefaults/string.yml

默认值

default: null
limit: 191
_null: false

../setting/Migrations/columnTypeDefaults/text.yml

默认值

default: null
_null: false

如果您在应用程序配置中提供选项,它们将重写默认值

初始化烘焙过程

<?php

require __DIR__ . '/vendor/autoload.php';

\Infotechnohelp\Bakery\Init\CakePhp::model("config/Bakery/init.model-schema.yml");

创建 config/Bakery/CakePhpBakedFiles/model.yml

- created/file/path
- created/file/path
- config\Bakery\CakePhpBakedFiles\model.yml
- config\Migrations\schema-dump-default.lock