简体   繁体   English

Kohana的。 对于每个ORM调用,都是一个数据库查询?

[英]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). 我想知道在Kohana中是否有针对每个ORM查询的一个数据库查询(例如MySQL)。

Example: 例:

What is more efficient? 有什么更有效的? This: 3 ORM calls 这: 3个ORM呼叫

$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 或者这样: 一个ORM调用并将其作为数组返回

$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. 仅使用几行来获取所有DB表将是低效率的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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