简体   繁体   中英

Kohana. For each ORM call is one database query?

I was wondering is there in Kohana for each ORM query one database query (eg 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! :)

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM