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

Hydrate Resultset using array instead of Pdo result

Is there any way to hydrate a Resultset/Simple using array instead of PDO result? I want to hydrate a resultset from cached associative array results. I use that array to hydrate resultsets with differents objects.

I saw a "TYPE_RESULT_FULL" on the resultset but I have no clue about how to use it.

    /**
     * Phalcon\Mvc\Model\Resultset\Simple constructor
     *
     * @param array $columnMap 
     * @param \Phalcon\Mvc\ModelInterface|Phalcon\Mvc\Model\Row $model 
     * @param \Phalcon\Db\Result\Pdo|null $result 
     * @param \Phalcon\Cache\BackendInterface $cache 
     * @param boolean $keepSnapshots 
     */
    public function __construct($columnMap, $model, $result, \Phalcon\Cache\BackendInterface $cache = null, $keepSnapshots = null) {}

Just save resultset in cache instead of array - https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model/resultset/simple.zep#L228 Or use cache service in phalcon. It won't be saved as object in cache, just array.



23.6k
Accepted
answer
edited Oct '16

I want to have different resultSets (each one with different mapping models) for the same result, so I ended extending Resulset/Simple in this way:

<?php
namespace Phalcon\Mvc\Model;

use Phalcon\Mvc\Model\Resultset\Simple;

class SimpleHidrator extends Simple
{
    public function hydrate($results)
    {
        $this->_rows = $results;
        $this->_count = count($results);
    }
}

and it works ok. I can pass an array to hydrate and the resultset becomes a real resulset simple:

        $results = [...]; // array of assoc arrays from database or cache
        $myModel = new MyModel();
        $hydrator = new SimpleHidrator(null, $myModel, null);
        $hydrator->hydrate($results); // hydrator becomes a resultset with MyModel instances
        $results = [...]; // array of assoc arrays from database or cache
        $otherModel = new OtherModel();
        $hydrator = new SimpleHidrator(null, $otherModel, null);
        $hydrator->hydrate($results); // hydrator becomes a resultset with OtherModel instances

Oh okay, sure, this is some solution :)