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

Model Aggregations methods + Cache

Hello everyone !

After successfully experimenting Phalcon cache (Memcache) on models with find / findFirst or with Models Manager, I'm facing a problem to cache properly aggregations !

An example :

$rows = MyModel::find(array("cache" => array("key" => "mykey", "lifetime" => 3600));

This one works like a charm.

But if i do this :

$rows = MyModel::sum(array("column" => "myfield", "cache" => array("key" => "mykey", "lifetime" => 3600));

This fails (like in all other calculations methods)

I'm in PHP 5.5.8 with Phalcon 1.2.5

Any idea of what's happening ?

Thanks !

edited Mar '14

Hi MyModel::sum() returns a scalar value, not a collection of objects. Use:

$key = "mykey";
$sum = $this->cache->get($key);
if($sum === null)
{
    $sum = MyModel::sum(array("column" => "myfield"));
    $this->cache->save($key, $sum, 3600);
}


3.2k

Thanks for the response.

I understand your point but in my opinion it doesn't matter if it is collection of objects or scalar value. It should be possible to cache the result like in the find / findFirst methods.

By the way if I do :

Mymodel::findFirst(array("column" => "sum(value)", "cache" => array("key" => "key", "lifetime" => 3600));

It works ! So why the shortcut method Mymodel::sum() doesn't ?