Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFR] Add getSql() to the query builder #1908

Closed
iby opened this issue Jan 25, 2014 · 1 comment
Closed

[NFR] Add getSql() to the query builder #1908

iby opened this issue Jan 25, 2014 · 1 comment

Comments

@iby
Copy link
Contributor

iby commented Jan 25, 2014

There are situations when using phql is not the preferred, but using query builder is useful. There seem to be no way of getting native sql out of it. The closest I got to is described in #1905 (comment), however that doesn't cover all scenarios.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@andresgutierrez
Copy link
Contributor

This is implemented in 2.0.3

<?php

namespace Some;

use Phalcon\DI,
    Phalcon\Db\Column,
    Phalcon\Mvc\Model,
    Phalcon\Events\Manager as EventsManager,
    Phalcon\Db\Adapter\Pdo\MySQL as Connection,
    Phalcon\Mvc\Model\Manager as ModelsManager,
    Phalcon\Mvc\Model\Metadata\Memory as ModelsMetaData;

$eventsManager = new EventsManager();

$di = new DI();

$connection = new Connection(array(
    "host"     => "localhost",
    "username" => "root",
    "password" => "",
    "dbname"   => "phalcon_test",
));

$connection->setEventsManager($eventsManager);

$eventsManager->attach('db',
    function ($event, $connection) {
        switch ($event->getType()) {
            case 'beforeQuery':
                echo $connection->getSqlStatement(), "\n";
                break;
        }
    }
);

$modelsManager = new ModelsManager();
$modelsManager->setDi($di);
$di['db'] = $connection;
$di['modelsManager'] = $modelsManager;
$di['modelsMetadata'] = new ModelsMetadata();

class Robots extends Model
{

}

print_r($modelsManager->createBuilder()
        ->from('Some\Robots')
        ->getQuery()
        ->getSql());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants