Cphalcon: Model::hasChanged($params) always return true if $params is array and any field is changed

Created on 1 Mar 2017  路  4Comments  路  Source: phalcon/cphalcon

Expected and Actual Behavior

Model::hasChanged() always return true if params is array and any field is changed.
I think if send array to model::hasChanged, this method should check that fields in this array has changed.

Something like this

public function hasChanged(var fieldName = null) -> boolean
    {
        var changedFields;

        let changedFields = this->getChangedFields();

        /**
         * If a field was specified we only check it
         */
        if typeof fieldName == "string" {
            return in_array(fieldName, changedFields);
        } elseif (typeof fieldName == "array") {
                       foreach (changedFields  as field) {
                            if in_array(field, changedFields) {
                                  return true;   
                            }                             
                       } 
                       return false;   
                }

        return count(changedFields) > 0;
    }
  • Phalcon version: 3.0.4
  • PHP Version: 7.0.15
  • Operating System: Win7
bug low

All 4 comments

I don't like this, maybe someone would like to check if only one of parameter of array was changed?

https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model.zep#L3824

Actually if array is provided nothing happens with it right now.

I would like to add argument strict, if false(by default?) it will do what you want, if true it will check all keys, like array_intersect.

Created some PR for this, let me know what you think.

This is what I needed. Thanks!

Fixed in the 3.2.x branch

Was this page helpful?
0 / 5 - 0 ratings