I want to get the latest data from the model after it has saved without doing another select.
Currently I do this:
if ($this->Model->save($data)){
$last = $this->Model->find('first',array(
'conditions' => array('Model.id' => $this->Model->id)
);
$last['Model']['dataChangedByBehaviors']; // <-- data I want
}
I want to get any data that was set in model callbacks or behaviors without performing an extra find.
There are two different situations for your example:
if ($this->Model->save($data)){
$data['Model']['dataChangeByBehaviors']; //---- I want get this
}
So, here the answer is: You already have the data.
(Note: If it's a new record, $data will of course not contain the ID, which you need to get from $this->Model->id. And if you are making any changes in the beforeSave() callback, these will of course not be reflected in your $data).
if ($this->Model->save($data)){
$last = $this->Model->read(null,$this->Model->id);
$last['Model']['dataChangeByBehaviors']; //---- I want get this
}
So here the answer is: There is no way of getting the data without a database request.
I'm not understand why people doing labor work. Just use getLastInsertId() CakePHP's inbuilt function and it's done :
$post_id=$this->Post->getLastInsertId();
If you're looking for some solution like if ($last = $this->Model->save($data)), I think there's no such thing as that.
But you can save some code using findById:
if ($this->Model->save($data)){
$last = $this->Model->findById($this->Model->id);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With