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

Caching of Phalcon\Mvc\Model\Resultset\Simple

Seems I am lost.

So, how to cache a Phalcon\Mvc\Model\Resultset\Simple query?

If I have something like this:


return new Simple(null, $this, $this->getReadConnection()->query($sql));

# and when I pass the cache object (although I am not sure what it does)

return new Simple(null, $this, $this->getReadConnection()->query($sql), $this->getDI()->get('cache'));

What actually happens when I pass the cache object and how can I name/get it later?

If I try to save the above return to a cache in controller, I get

Object of class Phalcon\Mvc\Model\Resultset\Simple could not be converted to string

Which seems right as there is no magic method. So, can it be cached from controller at all or how to do it?

edited May '16

Why you even doing it like this ? Why you just not use modelsManager or find findFirstt query from model class ? Simple is just resultset, the cache works on before select and then it creats simple resultset. You should really don't create simple object yourself beacause query from PHQL already returns it.

https://docs.phalcon.io/pl/latest/reference/models-cache.html#caching-phql-queries



12.2k
edited May '16

@Jurigag

It's a query where I use a subselect in FROM (and last time I checked it is not supported in phql)


SELECT x, y FROM (SELECT...) JOIN ...

And because of the above, I can't use:


$modelsManager = $this->getDI()->get('modelsManager');

$builder = $modelsManager->createBuilder();

$s = $builder->from(['alias' => \Phalcon\Db\Expression('FROM_SUBSELECT')])->join()....

Using find() and lazy loading each related record is kind of not an option.

So, my question is still, can it be done? And if yes, how?



145.0k
Accepted
answer
edited May '16

It is supported in phql using query builder or just builder.

Oh sorry i thought you mean just subselect, not in from.

So just just have raw select ? Just cache it yourself, i don't really see any other option.

edited May '16

I mean just access $this->getDI()->get('cache') and cache it there of course.

ALSO if you are using mysql yo for 90% already have mysql result cache, if it's just raw query which will don't need heavy orm things then you don't even need to cache it it's already cached.



12.2k

Actually, I was mistaken here. When I was saving to cache the result of the Phalcon\Mvc\Model\Resultset\Simple object I accidentally switched the places of needed variables:

/* In my controller */

$result = $model->findRecordsSimpleObject();

# this of course doesn't work
$this->cache->save($result, $cacheName, $seconds);

# this works
$this->cache->save($cacheName, $result, $seconds);

Your comment "Just cache it yourself" got me wondering, so I rechecked. Thx for that :)

I use https://api.phalcon.io/class/Phalcon/Cache/Backend/Libmemcached.html for this purpose. So yeah, you do whatever you think is the most approporiate for your use case, since you're the one who knows your data best.