当我在cakephp 3的表中同时具有x和x_id字段时,保存方法不起作用 [英] Save method don't work when I have both x and x_id fields in my table in cakephp 3
问题描述
我的用户表中同时有 language
和 language_id
字段。当我要保存数据时,保存方法返回 false
。
I have both language
and language_id
fields in my users table. When I want to save my data, save method returns false
.
//Table/UsersTable.php
$this->belongsTo('Languages', [
'alias' => 'Languages',
'foreignKey' => 'language_id'
]);
当我删除此代码或从数据库中删除语言字段时,保存方法将正常工作。
When I remove this code or remove language field from database, save method works properly.
推荐答案
是的,请记住CakePHP保留了一个属性名称,它将在其中存储每个关联的关联数据。对于您的情况,对于语言
关联,它将使用语言
属性。如果您已经有一个具有相同名称的字段,则可以配置关联以使用另一个属性名称:
Yeah, remember that CakePHP reserves the a property name where it will store the association data for each of your associations. In Your case, for the Languages
association it will use the language
property. If you already have a field with the same name, You can configure your association to use another property name:
$this->belongsTo('Languages', [
'alias' => 'Languages',
'foreignKey' => 'language_id',
'propertyName' => 'preferred_language'
]);
我个人会遵循惯例,不会使用语言$数据库中的c $ c>字段,当您已经有
language_id
字段时,就没有多大意义了。
Personally, I would stick to conventions and not have a language
field in the database, it make very little sense when you already have a language_id
field.
这篇关于当我在cakephp 3的表中同时具有x和x_id字段时,保存方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!