2
votes

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

1 Answers

2
votes

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

"