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

Custom caching of related records doesn't work

I am trying to implement caching of related records, I have two classes, say:

<pre> Car { initialize() { hasMany(Tyre); } }

Tyre { initialize() { belongsTo(Car) } } </pre>

I created a custom cache (Data frontend, File storage) and added it to $di

Now to save related records to this cache, I wrote:

$car = Car::findFirst(); $t = $car->getTyre(); $car->tyre = $t;

// push to cache $cache->save('a-key', $car, 900);

The problem is that the cache only stores Car object. The nested related Tyres are not there

What I am trying to do is very similar to this article - https://docs.phalcon.io/en/latest/reference/models-cache.html#caching-related-records-recursively, except that I want to store related records in a custom cache (instead of modelsCache).

Any help would be great! thanks



98.9k

What you need is intercept the call to getReusableRecords in the models manager and cache the related records: https://docs.phalcon.io/en/latest/reference/models-cache.html#reusable-related-records