模特的cakephp力量指数 [英] cakephp force index at model
问题描述
我在模型中使用了使用FORCE INDEX的find方法。模型很好,但是当我对该find方法进行测试时,发生了SQL错误。
我使用test / fixture并定义数据库架构和数据。在test / fixture中,我不知道如何定义索引。因此,DB for test没有索引。
如果您能告诉我如何在test / fixture中定义索引,那将会很棒。
I use find method which use "FORCE INDEX" in Model. Model is fine, but when I make a test for that find method, the SQL error happened. I use test/fixture and define DB schema and data. In the test/fixture, I don't know how to define index. Therefore, DB for test didn't have index. It would be great if you could show me how to define index in test/fixture.
在Model ...中
In Model...
$this->Model->find('all', array(
'fields' => array('foo'),
'conditions' => array('foo' => foo),
'joins' => array('FORCE INDEX(foo)'),
);
在测试/夹具中
var $fields = array(
'id' => ....
'foo' => ....
'created' => ....
'modified' => ....
);
推荐答案
我认为这会帮助你:
1-创建我自己的数据源 - (在我的情况下扩展DboMysql)
数据源有两个任务:
"1- Create my own datasource - (in my case extending DboMysql) data source has two tasks:
它覆盖读取方法并检查模型是否设置了useIndex字段
its overrides read method and checks if model has set useIndex field
if (!empty($model->useIndex)) {
$this->useIndex = $model->useIndex;
}
return parent::read($model, $queryData);
并且它会覆盖renderStatement方法,如果设置了$ model-> useIndex字段,则在表后添加其值select语句中的别名。
and it overrides renderStatement method and if $model->useIndex field was set, adding its value after table alias in select statement.
if (strtolower($type) == 'select' && !empty($this->useIndex)) {
$res = "SELECT {$fields} FROM {$table} {$alias} {$this->useIndex} {$joins} {$conditions} {$group} {$order} {$limit}";
} else {
$res = parent::renderStatement($type, $data);
}
$this->useIndex = null;
return $res;
2-设置模型字段whitch包含sql部分使用索引,强制索引或忽略索引
2- Setting up model field whitch contains sql part of use index, force index or ignore index
例如在控制器中:
$this->Task->useIndex = 'IGNORE INDEX(ind_usr_id)';
$this->paginate = array(
'fields' => array('Task.id', 'Task.name','User.id', 'User.name'),
'order' => 'Task.id',
'limit' => 10
);
$this->paginate('Task');
这篇关于模特的cakephp力量指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!