cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Phalcon 3.1 Model Changed Fields is not right for numeric string

Open leupom opened this issue 9 years ago • 11 comments

Hi, I just test the Phalcon 3.1 Model Changed Fields behavior. It is not right for numeric string, for example:

a int type column named "is_disabled" with value 1, after findFirst in PHP, it has converted to string type '1'.

now I invoke $model->is_disabled = 1, then getChangedFields and hasChanged contains the "is_disabled" field.

I suggest Model Changed Fields use rules: if model value or snapshot value is null or empty string, using type operator !== for changed, elses using normal operator !=. @bilikaz

Thanks, Phalcon Team. @sergeyklay which IDE when you write Zephir code, I would contribute some code to Phalcon, it is an essential framework. @dreamsxin PhpStorm 或其它 IDE 下能写 Zephir 代码吗?我是重度的 IDE 依赖者,不会用 vim 之类的编辑器,感觉很麻烦。你平时用什么开发环境提交 Zephir 代码的?

leupom avatar Mar 23 '17 02:03 leupom

我目前的话在用 Atom,在 Ubuntu 以及 Mac 下结合 GitHub Desktop 还是挺方便的。

dreamsxin avatar Mar 23 '17 06:03 dreamsxin

@leupom Could you please provide script to reproduce

sergeyklay avatar Mar 23 '17 06:03 sergeyklay

@sergeyklay https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model.zep#L3946, The values extracted from the database, It's always a string. So int 0 !== string '0'

dreamsxin avatar Mar 23 '17 07:03 dreamsxin

No, it's not always string, it's only your problem that you are not using mysqlnd.

Phalcon doesn't convert any values got from database to strings when not needed.

Jurigag avatar Mar 23 '17 08:03 Jurigag

use Pdo;
use Phalcon\Db\Adapter\Pdo\Mysql;

$class = new Mysql([
    'host'     =>  // ...
     // ...
    'options'  => [
        Pdo::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
        Pdo::ATTR_PERSISTENT         => false,
        Pdo::ATTR_EMULATE_PREPARES   => false, // this one
        Pdo::ATTR_STRINGIFY_FETCHES  => false, // and this one
    ]
]);

Izopi4a avatar Mar 23 '17 17:03 Izopi4a

@dreamsxin What about this https://forum.phalconphp.com/discussion/15529/bug-regarding-getchangedfields-and-haschanged#C45969

sergeyklay avatar Mar 23 '17 19:03 sergeyklay

He wrote, do some dirty check to check if it's null or empty string...

Jurigag avatar Mar 23 '17 19:03 Jurigag

Even when column contains real string with zero?

sergeyklay avatar Mar 23 '17 21:03 sergeyklay

My suggestion is that automatic conversion of data taken from the database, based on the field type, When enable_strict is true.

dreamsxin avatar Mar 24 '17 00:03 dreamsxin

@sergeyklay @Jurigag @Izopi4a I mean if a value in Model Snapshot is a numeric or numeric string, the Changed behavior is not precise. Like these: snapshot value is string '1', when change model field value to int 1; snapshot value is int 1, when change model field value to string '1';

snapshot value is string '1.20', when change model field value to float 1.2; snapshot value is float 1.2, when change model field value to string '1.20';

If using !== , these changed are true, but not right in PHP weakly-typed way. So I suggest just using !== , if model field or snapshot value is null or empty string, others keep != , it would satisfy most cases.

@dreamsxin 自动转换类型也不大对,字段值或快照值有 null 或 空字符串时,使用带类型的判断即可,其它情况保持 PHP 弱类型判断的默认行为。我现在继承 Phalcon 的 Model 类,自己重写了 hasChanged 和 getChangedFields 方法。

leupom avatar Mar 24 '17 02:03 leupom

@leupom If you simply configure your Mysql adapter with the options shown by lzopi4a your integers will be integers instead of strings to begin with. The Phalcon behavior is correct. This should be closed.

hakimio avatar Mar 24 '17 08:03 hakimio