Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i output/log the sql query generated by Fuel PHP's Orm queries?

Tags:

fuelphp

Is there some way of logging or outputting the mysql query string that is generated from a Model_Orm::query() or a Model_Orm::find()?

I have a complex Orm::query() that is not giving me the results i'd expect, and i'm wondering why!

like image 420
stef Avatar asked Apr 22 '13 11:04

stef


1 Answers

After doing anything which runs a query be it any of the following

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

You can use

echo \DB::last_query();

To see what was last run by whichever means 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'
like image 86
Ben Swinburne Avatar answered Jan 04 '23 06:01

Ben Swinburne