被黑方法(适用于 2.x 版)
转到 - /lib/Cake/Model/Datasource/DboSource.php 行 #942
修改函数 fullTableName 如下:
公共函数 fullTableName($model, $quote = true, $schema = true, $prefix = true) {
if (is_object($model)) {
$schemaName = $model->schemaName;
$table = $model->tablePrefix 。$模型->表;
} elseif (!empty($this->config['prefix']) && strpos($model, $this->config['prefix']) !== 0) {
$table = (((!empty($prefix) && $prefix !== true)?$prefix:(($prefix === false)?'':$this->config['prefix'] ))) . strval($模型);
} 别的 {
$table = strval($model);
}
if ($schema && !isset($schemaName)) {
$schemaName = $this->getSchemaName();
}
如果($报价){
if ($schema && !empty($schemaName)) {
if (strstr($table, '.') === false) {
返回 $this->name($schemaName) 。'。' . $this->name($table);
}
}
返回 $this->name($table);
}
if ($schema && !empty($schemaName)) {
if (strstr($table, '.') === false) {
返回 $schemaName 。'。' . $表;
}
}
返回$表;
}
现在转到同一页面上的第 1881 行并将函数 buildJoinStatement 修改为:
公共函数 buildJoinStatement($join) {
$data = array_merge(数组(
'类型' => 空,
'别名' => null,
'table' => 'join_table',
'条件' => '',
), $加入);
if (!empty($data['alias'])) {
$data['alias'] = $this->alias 。$this->name($data['alias']);
}
if (!empty($data['conditions'])) {
$data['conditions'] = trim($this->conditions($data['conditions'], true, false));
}
if (!empty($data['table']) && (!is_string($data['table']) || strpos($data['table'], '(') !== 0)) {
$data['table'] = (isset($data['prefix']))?$this->fullTableName($data['table'],true,true,$data['prefix']):$this ->fullTableName($data['table']);
}
返回 $this->renderJoinStatement($data);
}
而已...
现在,如果您在 joins 数组中提供前缀元素,那么它将对该表使用相同的前缀。
例子
'加入'=>数组(
大批(
'表'=>'GDN_Comment',
'类型'=>'左',
'前缀'=>假,
'别名'=>'评论',
'条件'=>数组('MyPost.DiscussionID = 2'),
),
),
然后它将在sql查询中使用“GDN_Comment as Comment”
或者
'加入'=>数组(
大批(
'表'=>'GDN_Comment',
'类型'=>'左',
'前缀'=>'ds_',
'别名'=>'评论',
'条件'=>数组('MyPost.DiscussionID = 2'),
),
),
然后它将在sql查询中使用“ds_GDN_Comment as Comment”