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

Question about how can I know findFirst returns result in cache?

use Model::find() returns resultset,and I can use resultset->isFresh() to get if resultset is cached!

but findFirst returns resultset->getFirst() and cannot call isFresh method,so I dont know findFirst gets from cache or from db directly?

let result = cache->get(key, lifetime);

if result !== null {

if typeof result != "object" {

          throw new Exception("Cache didn't return a valid resultset");

      }

      result->setIsFresh(false);

      /**

       * Check if only the first row must be returned

       */

      if uniqueRow {

          let preparedResult = result->getFirst();

      } else {

          let preparedResult = result;

      }

      return preparedResult;

  }

You have to use a query builder

$query = \Phalcon\Di::getDefault()->modelsManager
    ->createBuilder()
    ->from('Robots')
    ->where('id = {id:int}')
    ->getQuery()
    ->setUniqueRow();

$robot = $builder->execute(['id' => 123]);

$builder->isFresh(); // my robot too ;)

Good luck



989

I know this must be a good solution! But this will change too many codes!

And I have tried to change findFirst to Mode::find() -> getFirst(), but there are too many errors,like column not found,etc.!

function findFirst($parameter=null) {

$result = parent::find($parameter) ;

if ($result->count()<1) {

return false;

}

$is_fresh = $result->isFresh(); // detect here

$row = $result->getFirst();

return $row;

}

By the way, how to cache model's data instead of cacheing model structure and data.(model structure and data is too big.)

You have to use a query builder

$query = \Phalcon\Di::getDefault()->modelsManager
  ->createBuilder()
   ->from('Robots')
   ->where('id = {id:int}')
   ->getQuery()
  ->setUniqueRow();

$robot = $builder->execute(['id' => 123]);

$builder->isFresh(); // my robot too ;)

Good luck

What's your goal? Why do you want to know if a model is cached or not?