为什么 Cake Bake 不将实体的 User 复数化?
Why does Cake Bake don't pluralize User at entity?
我有一个名为 users
的模型和另一个名为 permissions
的模型。我刚刚用 cake bake model all
生成了两个,我得到了这个:
用户实体:
protected $_accessible = [
'email' => true,
'password' => true,
'permissions' => true, // Plural
];
许可实体:
protected $_accessible = [
'user_id' => true,
'controller' => true,
'user' => true, // Singular
];
这是 Bake
错误还是背后有某种逻辑?
答案在 CakePHP 3 文档中解释,在 Inflection Rules Updated
下
BelongsTo and HasOne associations will use the singular form in entity
properties, while HasMany and BelongsToMany (HABTM) will use plural
forms.
换句话说,user
是单数,因为它与 belongsTo / hasOne 关联相关。 permissions
是复数,因为它与 hasMany 或 HABTM 关联有关。
如果人们从 CakePHP 2 迁移过来,这可能会让他们措手不及,但这实际上非常直观。每个权限都有一个 用户 被授予,但每个用户可能有多个 权限 被授予。
我有一个名为 users
的模型和另一个名为 permissions
的模型。我刚刚用 cake bake model all
生成了两个,我得到了这个:
用户实体:
protected $_accessible = [
'email' => true,
'password' => true,
'permissions' => true, // Plural
];
许可实体:
protected $_accessible = [
'user_id' => true,
'controller' => true,
'user' => true, // Singular
];
这是 Bake
错误还是背后有某种逻辑?
答案在 CakePHP 3 文档中解释,在 Inflection Rules Updated
下BelongsTo and HasOne associations will use the singular form in entity properties, while HasMany and BelongsToMany (HABTM) will use plural forms.
换句话说,user
是单数,因为它与 belongsTo / hasOne 关联相关。 permissions
是复数,因为它与 hasMany 或 HABTM 关联有关。
如果人们从 CakePHP 2 迁移过来,这可能会让他们措手不及,但这实际上非常直观。每个权限都有一个 用户 被授予,但每个用户可能有多个 权限 被授予。