当我在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

查看:57
本文介绍了当我在cakephp 3的表中同时具有x和x_id字段时,保存方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户表中同时有 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'
]);

我个人会遵循惯例,不会使用语言字段,当您已经有 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆