We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Can model keepsnapshots only on update ?

I want to know what exactly keepsnapshots do in the model. Could this make only on update snapshot, because i use a lot of ModelName::find() without update. I couldn't need snapshot for this action and this will slow my application performance and will produce more Memory consumption which is not neccessary. I want only when I update information to keepsnapshot and use my Blame Class to save changed fields. Is it possible ?



2.1k
$this->useDynamicUpdate(true);


1.7k

useDynamicUpdate will make snapshot? It is possible to make $this->keepsnapshots (true) in beforeUpdate Method. I think that this will make.snapshot only when I update record. Is it true?



2.1k
edited Jan '15

dynamic update will force the update to be updating only the fields that have changed. so when you make your blameable class track the query used, it will only show the fields that have changed. meaning you have to rewrite that class to hijack and store the query used. which is different from the blame class provided.

if you only want a snapshot only when updating, sure you can do it too.

public function beforeSave()
{
    if($id != 0)
    {
        $data = $this->findFirst($id)->toArray();
        $this->setSnapshotData($data);
    }
}

and viola. however if you do this. there will be more cycle consumption, both web server and database. which will really slow your application down. because it has to retrieve the data from database AGAIN. however if you use the snapshot data when you first retrieve it there is no additional cycle. just memory consumption.

so its really up to you. and seriously unless your model is stupid huge, the consumption of keeping the snapshot when it is first retrieve would be a few bytes the most. also if your model is stupid huge that means you really need to work on designing your database model. normalization.

Imo, stick to snapshot from retriving.



1.7k
edited Jan '15

Ok I understand but i have another question :)

If i set: public function initialize() { $this->keepSnapshots(true); }

This will make snapshot every time when i use ModelName::findFirst(); or ModelName::find(); When the snapshot will be removed from memory ? When this snapshot will expire ?

Sorry for my questions but i want to understand how keepsnapshot works and according to this to make a decision to implement or not Blame class in my application.



2.1k

I think you misunderstand how php works.

The only thing that is kept is the info in session, again those are either in memory/files. The rest of everything is brand new. Every single time you go to a different page, the entire application is cleared from memory, and reinitialized again. = ="

request1 start request1 finished exit request2 start request2 finished exit.



1.7k

I know how php works and now I understood how snapshot works :) . It keep snapshot of the model only when is executing. Thanks alot for your help.