Skip to content

Loading…

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

Closed
ianbytchek opened this Issue · 1 comment

3 participants

@ianbytchek

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.

@niden niden added the Unassigned label
@andresgutierrez
Collaborator

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
Something went wrong with that request. Please try again.