Model snapshots doesn't work with cache #12000

Closed
udarkness opened this Issue Jul 20, 2016 · 5 comments

Projects

None yet

3 participants

@udarkness
udarkness commented Jul 20, 2016 edited
public function initialize()
{
    $this->keepSnapshots(true);
}

works fine but when using it with the caching technique from this page: https://docs.phalconphp.com/en/latest/reference/models-cache.html#caching-resultsets
it produces an error when calling the method hasChanged.
I guess the cache is not taking the snapshot when it restores the data.
The user has to manually call $model->setSnapshotData($model->toArray()), but this is a workaround that should not be required.

@udarkness udarkness changed the title from Model napshots doesn't work with cache to Model snapshots doesn't work with cache Jul 22, 2016
@sergeyklay sergeyklay self-assigned this Aug 12, 2016
@sergeyklay
Collaborator

@udarkness Could you please provide script to reproduce?

@udarkness

Sure, create a new project with the phalcon tools,
add this to the index controller:

<?php

class IndexController extends ControllerBase
{

    public function indexAction()
    {
        $site = Site::findFirst(array(
                "cache" => array(
                    "key" => "somecache",
                    "lifetime" => 3002
                )
        ));
        $site->hasChanged();
        var_dump($site);
        die();
    }

}

Add the modelCache for redis:

$di->set('modelsCache', function () use ($config) {

    // Cache data for one day by default
    $frontCache = new \Phalcon\Cache\Frontend\Data([
        "lifetime" => 33000,
        ]
    );

    // Memcached connection settings
    $cache = new Phalcon\Cache\Backend\Redis($frontCache, array(
        'prefix' => 'orm:',
        'host' => '127.0.0.1',
    ));

    return $cache;
});

Create a dummy table/model Site, add a record in the DB and this in the model

public function initialize()
{
    $this->keepSnapshots(true);
}

Run once and it is flawless, even the hasChanged works fine if you modify or not.
Run again, and kaput! you will get

The record doesn't have a valid data snapshot

#0 /var/www/html/demo/app/controllers/IndexController.php(14): Phalcon\Mvc\Model->hasChanged()
#1 [internal function]: IndexController->indexAction()
#2 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(IndexController), 'indexAction', Array)
#3 [internal function]: Phalcon\Dispatcher->_dispatch()
#4 [internal function]: Phalcon\Dispatcher->dispatch()
#5 /var/www/html/demo/public/index.php(29): Phalcon\Mvc\Application->handle()
#6 {main}

The solution sounds easy, I don't know if the implementation is :), it is just to take the snapshot when the object wakes up

@Jurigag Jurigag added a commit to Jurigag/cphalcon that referenced this issue Aug 26, 2016
@Jurigag Jurigag Fix #12000, #12170 a59a558
@Jurigag Jurigag added a commit to Jurigag/cphalcon that referenced this issue Aug 26, 2016
@Jurigag Jurigag Fix #12000, #12170 f361479
@Jurigag Jurigag added a commit to Jurigag/cphalcon that referenced this issue Aug 26, 2016
@Jurigag Jurigag Fix #12000, #12170 60538a6
@Jurigag Jurigag added a commit to Jurigag/cphalcon that referenced this issue Aug 26, 2016
@Jurigag Jurigag Fix #12000, #12170 7adb70a
@Jurigag Jurigag added a commit to Jurigag/cphalcon that referenced this issue Aug 26, 2016
@Jurigag Jurigag Fix #12000, #12170 676fd2f
@sergeyklay sergeyklay modified the milestone: 3.1.0, 3.0.2 Aug 26, 2016
@sergeyklay
Collaborator
sergeyklay commented Aug 26, 2016 edited

@udarkness
Could you please check 3.0.x branch?

git clone git@github.com:phalcon/cphalcon.git
cd cphalcon
git checkout 3.0.x

zephir fullclean
zephir build
@dschissler
Contributor

Nice one jurigag.

@udarkness

Works great @sergeyklay, thank you @Jurigag

@sergeyklay sergeyklay closed this Aug 29, 2016
@sergeyklay sergeyklay added a commit to sergeyklay/cphalcon that referenced this issue Sep 27, 2016
@Jurigag @sergeyklay Jurigag + sergeyklay Fix #12000, #12170 93483f8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment