cakephp force index at model

佐手、 提交于 2019-12-12 18:33:40

问题


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.

In Model...

$this->Model->find('all', array(
     'fields' => array('foo'),
     'conditions' => array('foo' => foo),
     'joins' => array('FORCE INDEX(foo)'),
);

In test/fixture

var $fields = array(
    'id' => ....
    'foo' => ....
    'created' => ....
    'modified' => ....
 );

回答1:


i think that ll help u:

http://cakephp.1045679.n5.nabble.com/Using-USE-INDEX-or-FORCE-INDEX-in-Model-gt-find-amp-relationships-td3281552.html#a3300205

"1- Create my own datasource - (in my case extending DboMysql) data source has two tasks:

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

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- Setting up model field whitch contains sql part of use index, force index or ignore index

eg in controller:

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

"



来源:https://stackoverflow.com/questions/15949720/cakephp-force-index-at-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!