Mirgen
Mirgen

Reputation: 170

Kohana. For each ORM call is one database query?

I was wondering is there in Kohana for each ORM query one database query (e.g. MySQL).

Example:

What is more efficient? This: 3 ORM calls

$title = ORM::factory('Setting')->where('name', '=', 'title')->find()->value;
$keywords    = ORM::factory('Ssetting')->where('name', '=', 'keywords')->find()->value;
$description = ORM::factory('Setting')->where('name', '=', 'description')->find()->value;

Or this: One ORM call and return it as array

$settings = ORM::factory('Setting')->find_all()->as_array('name', 'value');
$title = $settings['title'];
$keywords    = $settings['keywords'];
$description = $settings['description'];

Thank you! :)

Upvotes: 0

Views: 195

Answers (1)

MaGnetas
MaGnetas

Reputation: 5008

Insert this after your code:

echo View::factory('profiler/stats');

It will show you all queries being used (and a count next to each of them). Profiling should be enabled in your bootstrap init though. Don't forget to disable this before going live. More of it here

Anyway, in your case the first method will execute three queries and the second one only one. But what if there are more settings than you need? It would be inefficient to get all DB table just to use a few rows.

You could do something like this:

ORM::factory('Setting')->
    where_open()->
        or_where('name', '=', 'title')->
        or_where('name', '=', 'keywords')->
        or_where('name', '=', 'description')->
    where_close()->
    find_all()->
    as_array('name', 'value');

Or even:

ORM::factory('Setting')->
    where('name', 'IN', array('title', 'keywords', 'description'))->
    find_all()->
    as_array('name', 'value');

Upvotes: 1

Related Questions