lox / pheasant

A lightweight data mapper designed to take advantage of PHP 5.3+
http://getpheasant.com
MIT License
101 stars 21 forks source link

$this->changes() does not behave as expected #60

Closed jorisleker closed 11 years ago

jorisleker commented 11 years ago

We want to use $this->changes() to trigger some business logic when editing/saving domain objects (using before/afterSave hooks). We found that we can't use the changes() method to find out whether properties were changed.

The changes() method does not really reflect changes to the domainObject, because any prop that was touched is returned, even it's was not actually changed (a more appropriate name would be touched()).

We propose changing the set() method to only add properties to $this->_changed[] when they were actually changed.

So change this:

    /**
     * Sets a property
     */
    public function set($prop, $value)
    {
        $this->_data[$prop] = $value;
        $this->_changed[] = $prop;

        return $this;
    }

into this:

    /**
     * Sets a property
     */
    public function set($prop, $value)
    {
        if($this->_data[$prop] != $value) 
        {
            $this->_data[$prop] = $value;
            $this->_changed[] = $prop;
        }

        return $this;
    }
lox commented 11 years ago

Yup, that's probably reasonable. Are you able to create a pull request?