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

PHQL in a non Phalcon project

Hello guys, I have tried to find a similar issue, but I may either use wrong search criteria or this is just not in the set of questions people usually ask.

So I love Phalcon and I wish I could use it everywhere. But when you have to work in an old project (legacy) is not easy to do a full migration. But PhalconPHP is very decoupled, which is on of the awesome desicions the lead developers have taken. So here is my question.

I have a project that doesn't use an standard ORM. What we have, works, but it could be better. I really like the way that phalcon uses Models/PHQL. It's very easy to learn how to use it, and the performance is excellent. I have had in the past very horrible results using Doctrine. The idea of Doctrine is noble, but the implementation... Well, the hydratation process is super expensive, and if you are dealing with big set of data, it just doesn't fit to our needs.

I'd love to use the PHQL in my project. I could actually migrate the models, it wouldn't be that hard.

Do you know if there is any way that I can just use the Phalcon/MVC/Model+PHQL without to have to use the entire framework?

I am using a dependency injection, and I know that the way to tie things together in phalcon is throgh the Phalcon DI. So, if I am using Pimple as DI, would it be possible to use PHQL??

If so, how?

Thanks!



51.2k

I don't know exactly how, but this might help: Inject in your DI a configuration containg database connection info:


$config = new \Phalcon\Config([
    'database' => array(
        'adapter'  => 'Mysql',
        'host'     => 'localhost',
        'username' => '',
        'password' => '',
        'dbname'   => '',
    )
]);

$di['config'] = $config;

Then use your models:

CarModel extends \Phalcon\Mvc\Model
{

}

$car = new CarModel();
$car->setDI($di);

print_r($car->findFirst()->toArray());

You might want/need to inject also something similar:

$di['cache'] = function () use ($di, $config) {

    $frontend = new \Phalcon\Cache\Frontend\Igbinary(array(
        'lifetime' => 3600 * 24
    ));

    $cache = new \Phalcon\Cache\Backend\Redis($frontend, array(
        'redis' => $di['redis'],
        'prefix' => $config->session->name.':'
    ));

    return $cache;
};

$di['modelsManager'] = function () {
    return new \Phalcon\Mvc\Model\Manager();
};

$di['modelsCache'] = $di['cache'];


5.8k
Accepted
answer
edited Apr '15

Wow man You are pretty cool!

So it is possible. I had to define the Phalcon DI, the modelsMetadata and the db in the Phalcon DI, but works.

<?php

//Insert yout previous code in here, same model declaration and maybe just define the getSource method in the model

$di = new \Phalcon\DI();
$di->set('modelsManager', function() {
    return new \Phalcon\Mvc\Model\Manager();
});
$di->set('modelsMetadata', function ()
{
    return new \Phalcon\Mvc\Model\MetaData\Memory();
});
$di->set('db', function(){
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
       'host' => 'y.y.y.y',
       'username' => 'yyyy',
       'password' => 'xxxxx',
       'dbname' => 'xxx',
       'port' => '3306'
    ));
});

Awesome!