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

How to combine 2 data sets into one paginator

Hi, everyone ! I try to comebine 2 data sets into one paginator. For example:

$result1 = modelName1::find();

$result2 = modelName2::find();

$result = $result1 + $result2;

$paginator = new Phalcon\Paginator\Adapter\Model(array( "data" => $result, "limit" => 12, "page" => $numberPage ));

Could anyone help me to do that? Thank you very much !



3.5k
Accepted
answer
edited Mar '15

You have two options: you could make a phql sintax to get merged resultset, for example:

this code in model

    public function getMergedResultSet()
    {
            $phql = "SELECT
                              model1.col1,
                              model1.col2,
                              model2.col1,
                              model2.col2
                           FROM
                              model1,
                              model2
            ";
            $query = $this->getDi()->getShared("modelsManager")->createQuery($phql);

            return $query->execute();
    }

for execute modelsManager phql you should add the next syntax in bootstrap file DI:

$di = new Phalcon\DI();

$di->set('modelsManager', function() { return new Phalcon\Mvc\Model\Manager(); });

other option is array_merge function, but i like the first.