Sunday, June 30, 2013

Re: Get old record in beforeSave when updating (doing a $this->find() inside beforeSave callback)



On Saturday, 29 June 2013 21:14:31 UTC+2, Vanja Dizdarević wrote:
Thanks MorFer, that is a simple solution, but it feels like meddling with Model. At the point of the find, the model is in the save cycle and I don't know what else a find() might change in it.

Btw, $this->data AND $this->id should be saved. The id should stay the same, but you never know.

This feels cleaner, but I'm wondering about the overhead:

            $Model = ClassRegistry::init('User');
            $query = [
                 'recursive' => 0, 
                 'conditions' => [$this->alias.'.'.$this->primaryKey => $this->data[$this->alias][$this->primaryKey]]
            ];
            $oldData = $Model->find('first', $query);

If you do that inside the User model - $this and $Model are exactly the same object.

AD

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: