How can I output / register an SQL query generated by Fuel PHP Orm queries?

Is there a way to register or output the mysql query string that is generated from Model_Orm::query()or Model_Orm::find()?

I have a complex Orm::query()one that does not give me the expected results, and I wonder why!

+5
source share
3 answers

After doing anything that executes the request, there will be any of the following

\Model_Something::find('all');
\Model_Something::query()...
\DB::query()...
\DB::select()...

you can use

echo \DB::last_query();

To find out what was last run depending on what you used to query the database.

Working example taken from production code:

$product = Model_Product::find('all', array(
     'related' => array(
              'i18ns',
     ),
     'where' => array(
              array('i18ns.variant_slug', $slug)
     )
));

echo \DB::last_query();
exit;

SELECT `t0`.`family_id` AS `t0_c0`, `t0`.`type_id` AS `t0_c1`,
       `t0`.`sort_weight` AS `t0_c2`, `t0`.`active` AS `t0_c3`,
       `t0`.`attribute_set` AS `t0_c4`, `t0`.`created_at` AS `t0_c5`,
       `t0`.`updated_at` AS `t0_c6`, `t0`.`site_id` AS `t0_c7`,
       `t0`.`temporal_start` AS `t0_c8`, `t0`.`temporal_end` AS `t0_c9`,
       `t0`.`id` AS `t0_c10`, `t1`.`id` AS `t1_c0`, `t1`.`product_id` AS `t1_c1`,
       `t1`.`language_id` AS `t1_c2`, `t1`.`slug` AS `t1_c3`,
       `t1`.`variant_slug` AS `t1_c4`, `t1`.`title` AS `t1_c5`,
       `t1`.`description` AS `t1_c6`, `t1`.`temporal_start` AS `t1_c7`,
       `t1`.`temporal_end` AS `t1_c8`
FROM `products` AS `t0`
LEFT JOIN `product_i18n` AS `t1`
     ON (`t0`.`id` = `t1`.`product_id`)
WHERE `t1`.`variant_slug` = 'test-product'
+9
source

. , , get_query(); ORM.

, .

$queryToExecute = Model_Article::query()
        ->select('author')
        ->where('date', '<', time())
        ->where('draft', '=', 1);


// This will print the query which is actually executed by your ORM 
echo $queryToExecute->get_query();

$queryString = $queryToExecute->get_query();
Log::info("Query that is executed {$queryString} ");

, , .

, .

+2

After completing the request, you can use the following statement

echo \Database_Connection::instance(Model_Foo::$_connection)->last_query;

0
source

All Articles