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

Is this tutorial section about Caching PHQL wrong?

Hi,

When apply caching PHQL following https://docs.phalcon.io/en/latest/reference/models-cache.html#caching-of-phql-planning, I see the tutorial may be wrong. Because I just a Phalcon newbie, it's take me a lot of time to find out what happening...

In the tutorial:

<?php

$phql  = "SELECT * FROM Store\Robots WHERE id = ?0";
$query = $this->modelsManager->createQuery($phql);

for ($i = 1; $i <= 10; $i++) {

    $robots = $query->execute($phql, array($i));

    // ...
}

But I think it must be:

<?php

$phql  = "SELECT * FROM Store\Robots WHERE id = ?0";
$query = $this->modelsManager->createQuery($phql);

for ($i = 1; $i <= 10; $i++) {

    $robots = $query->execute(array($i));

    // ...
}

Is that right?

Beside of that, in this loop case, if we use the syntax like Robots::find($i), can we cache them?



85.5k
edited Aug '16

for caching phql i think you need to use it like this:


$phql = "SELECT * FROM Cars WHERE name = :name:";

$query = $this->modelsManager->createQuery($phql);

$query->cache(
    array(
        "key"      => "cars-by-name",
        "lifetime" => 300
    )
);

$cars = $query->execute(
    array(
        'name' => 'Audi'
    )
);

liek it says on the docs.

For caching ORM i use it like this


$products = Products::find(
    array(
        "cache" => array(
            "key" => "my-cache"
        )
    )
);

For me using Model in queries is because if you deside to change the table name, then all you need to do is it change it in the model and not in other 1000 polaces aswell.



6.0k
edited Aug '16

Thank @izo,

But I mean the tutorial may be wrong in this line:

$robots = $query->execute($phql, array($i));

It should be:

$robots = $query->execute(array($i));



85.5k

based on this https://github.com/phalcon/cphalcon/blob/5372ac7972218b10fd9f08027a7718ea10f191b5/phalcon/mvc/model/queryinterface.zep#L75 probably you are right. Dont know to be honest, I use query builder in a different way and I haven't actually used it recently.

But you can test it yourself, if you see the example is wrong, here is the docs repo: https://github.com/phalcon/docs

its quite easy to navigate inside. You can easily contribute to phalcon :-)

edited Aug '16

@Izo

He doesn't mean a result cache query. There is built-in cache for phql queries - so they don't need to be rebuilded to the final sql form. Just for example:

$ids = [1, ...,100];
$phql = "SELECT * FROM SomeModel WHERE id = :id:";

If we use bound parameters, then PHQL query will be build just once for all ids. This is what docs mention kind of.

Caching phql is built-in thing, no need for caching it, caching resultset is whole diffrent thing.