Merge pull request #2289 from bar/2.5-optimize-dbosource

First part of the DboSource cleanup.
This commit is contained in:
José Lorenzo Rodríguez 2013-11-21 01:02:36 -08:00
commit df5fc2304a
3 changed files with 528 additions and 491 deletions

View file

@ -183,12 +183,12 @@ class DataSource extends Object {
*
* To-be-overridden in subclasses.
*
* @param Model $model The Model to be created.
* @param Model $Model The Model to be created.
* @param array $fields An Array of fields to be saved.
* @param array $values An Array of values to save.
* @return boolean success
*/
public function create(Model $model, $fields = null, $values = null) {
public function create(Model $Model, $fields = null, $values = null) {
return false;
}
@ -197,12 +197,12 @@ class DataSource extends Object {
*
* To-be-overridden in subclasses.
*
* @param Model $model The model being read.
* @param Model $Model The model being read.
* @param array $queryData An array of query data used to find the data you want
* @param integer $recursive Number of levels of association
* @return mixed
*/
public function read(Model $model, $queryData = array(), $recursive = null) {
public function read(Model $Model, $queryData = array(), $recursive = null) {
return false;
}
@ -211,13 +211,13 @@ class DataSource extends Object {
*
* To-be-overridden in subclasses.
*
* @param Model $model Instance of the model class being updated
* @param Model $Model Instance of the model class being updated
* @param array $fields Array of fields to be updated
* @param array $values Array of values to be update $fields to.
* @param mixed $conditions
* @return boolean Success
*/
public function update(Model $model, $fields = null, $values = null, $conditions = null) {
public function update(Model $Model, $fields = null, $values = null, $conditions = null) {
return false;
}
@ -226,11 +226,11 @@ class DataSource extends Object {
*
* To-be-overridden in subclasses.
*
* @param Model $model The model class having record(s) deleted
* @param Model $Model The model class having record(s) deleted
* @param mixed $conditions The conditions to use for deleting.
* @return boolean Success
*/
public function delete(Model $model, $conditions = null) {
public function delete(Model $Model, $conditions = null) {
return false;
}
@ -319,94 +319,89 @@ class DataSource extends Object {
* @param string $query Query string needing replacements done.
* @param array $data Array of data with values that will be inserted in placeholders.
* @param string $association Name of association model being replaced
* @param array $assocData
* @param Model $model Instance of the model to replace $__cakeID__$
* @param Model $linkModel Instance of model to replace $__cakeForeignKey__$
* @param Model $Model Model instance
* @param array $stack
* @return string String of query data with placeholders replaced.
*/
public function insertQueryData($query, $data, $association, $assocData, Model $model, Model $linkModel, $stack) {
public function insertQueryData($query, $data, $association, Model $Model, $stack) {
$keys = array('{$__cakeID__$}', '{$__cakeForeignKey__$}');
foreach ($keys as $key) {
$val = null;
$type = null;
$modelAlias = $Model->alias;
if (strpos($query, $key) !== false) {
foreach ($keys as $key) {
if (strpos($query, $key) === false) {
continue;
}
$insertKey = $InsertModel = null;
switch ($key) {
case '{$__cakeID__$}':
if (isset($data[$model->alias]) || isset($data[$association])) {
if (isset($data[$model->alias][$model->primaryKey])) {
$val = $data[$model->alias][$model->primaryKey];
} elseif (isset($data[$association][$model->primaryKey])) {
$val = $data[$association][$model->primaryKey];
}
} else {
$found = false;
foreach (array_reverse($stack) as $assoc) {
if (isset($data[$assoc]) && isset($data[$assoc][$model->primaryKey])) {
$val = $data[$assoc][$model->primaryKey];
$found = true;
break;
}
}
if (!$found) {
$val = '';
}
}
$type = $model->getColumnType($model->primaryKey);
$InsertModel = $Model;
$insertKey = $Model->primaryKey;
break;
case '{$__cakeForeignKey__$}':
foreach ($model->associations() as $name) {
foreach ($model->$name as $assocName => $assoc) {
if ($assocName === $association) {
if (isset($assoc['foreignKey'])) {
$foreignKey = $assoc['foreignKey'];
$assocModel = $model->$assocName;
$type = $assocModel->getColumnType($assocModel->primaryKey);
foreach ($Model->associations() as $type) {
foreach ($Model->{$type} as $assoc => $assocData) {
if ($assoc !== $association) {
continue;
}
if (isset($data[$model->alias][$foreignKey])) {
$val = $data[$model->alias][$foreignKey];
} elseif (isset($data[$association][$foreignKey])) {
$val = $data[$association][$foreignKey];
} else {
$found = false;
foreach (array_reverse($stack) as $assoc) {
if (isset($data[$assoc]) && isset($data[$assoc][$foreignKey])) {
$val = $data[$assoc][$foreignKey];
$found = true;
break;
}
}
if (!$found) {
$val = '';
}
}
if (isset($assocData['foreignKey'])) {
$InsertModel = $Model->{$assoc};
$insertKey = $assocData['foreignKey'];
}
break 3;
}
}
}
break;
}
$val = $dataType = null;
if (!empty($insertKey) && !empty($InsertModel)) {
if (isset($data[$modelAlias][$insertKey])) {
$val = $data[$modelAlias][$insertKey];
} elseif (isset($data[$association][$insertKey])) {
$val = $data[$association][$insertKey];
} else {
$found = false;
foreach (array_reverse($stack) as $assocData) {
if (isset($data[$assocData]) && isset($data[$assocData][$insertKey])) {
$val = $data[$assocData][$insertKey];
$found = true;
break;
}
}
if (!$found) {
$val = '';
}
}
$dataType = $InsertModel->getColumnType($InsertModel->primaryKey);
}
if (empty($val) && $val !== '0') {
return false;
}
$query = str_replace($key, $this->value($val, $type), $query);
}
$query = str_replace($key, $this->value($val, $dataType), $query);
}
return $query;
}
/**
* To-be-overridden in subclasses.
*
* @param Model $model Model instance
* @param Model $Model Model instance
* @param string $key Key name to make
* @return string Key name for model.
*/
public function resolveKey(Model $model, $key) {
return $model->alias . $key;
public function resolveKey(Model $Model, $key) {
return $Model->alias . $key;
}
/**

View file

@ -634,6 +634,7 @@ class DboSource extends DataSource {
/**
* Returns an array of all result rows for a given SQL query.
*
* Returns false if no rows matched.
*
* ### Options
@ -711,13 +712,16 @@ class DboSource extends DataSource {
if (strpos($field, $this->virtualFieldSeparator) === false) {
continue;
}
list($alias, $virtual) = explode($this->virtualFieldSeparator, $field);
if (!ClassRegistry::isKeySet($alias)) {
return;
}
$model = ClassRegistry::getObject($alias);
if ($model->isVirtualField($virtual)) {
$Model = ClassRegistry::getObject($alias);
if ($Model->isVirtualField($virtual)) {
$result[$alias][$virtual] = $value;
unset($result[0][$field]);
}
@ -945,6 +949,7 @@ class DboSource extends DataSource {
} else {
$table = strval($model);
}
if ($schema && !isset($schemaName)) {
$schemaName = $this->getSchemaName();
}
@ -957,11 +962,13 @@ class DboSource extends DataSource {
}
return $this->name($table);
}
if ($schema && !empty($schemaName)) {
if (strstr($table, '.') === false) {
return $schemaName . '.' . $table;
}
}
return $table;
}
@ -970,45 +977,47 @@ class DboSource extends DataSource {
*
* Creates new records in the database.
*
* @param Model $model Model object that the record is for.
* @param array $fields An array of field names to insert. If null, $model->data will be
* @param Model $Model Model object that the record is for.
* @param array $fields An array of field names to insert. If null, $Model->data will be
* used to generate field names.
* @param array $values An array of values with keys matching the fields. If null, $model->data will
* @param array $values An array of values with keys matching the fields. If null, $Model->data will
* be used to generate values.
* @return boolean Success
*/
public function create(Model $model, $fields = null, $values = null) {
public function create(Model $Model, $fields = null, $values = null) {
$id = null;
if (!$fields) {
unset($fields, $values);
$fields = array_keys($model->data);
$values = array_values($model->data);
$fields = array_keys($Model->data);
$values = array_values($Model->data);
}
$count = count($fields);
for ($i = 0; $i < $count; $i++) {
$valueInsert[] = $this->value($values[$i], $model->getColumnType($fields[$i]));
$valueInsert[] = $this->value($values[$i], $Model->getColumnType($fields[$i]));
$fieldInsert[] = $this->name($fields[$i]);
if ($fields[$i] == $model->primaryKey) {
if ($fields[$i] == $Model->primaryKey) {
$id = $values[$i];
}
}
$query = array(
'table' => $this->fullTableName($model),
'table' => $this->fullTableName($Model),
'fields' => implode(', ', $fieldInsert),
'values' => implode(', ', $valueInsert)
);
if ($this->execute($this->renderStatement('create', $query))) {
if (empty($id)) {
$id = $this->lastInsertId($this->fullTableName($model, false, false), $model->primaryKey);
$id = $this->lastInsertId($this->fullTableName($Model, false, false), $Model->primaryKey);
}
$model->setInsertID($id);
$model->id = $id;
$Model->setInsertID($id);
$Model->id = $id;
return true;
}
$model->onError();
$Model->onError();
return false;
}
@ -1017,99 +1026,106 @@ class DboSource extends DataSource {
*
* Reads record(s) from the database.
*
* @param Model $model A Model object that the query is for.
* @param Model $Model A Model object that the query is for.
* @param array $queryData An array of queryData information containing keys similar to Model::find()
* @param integer $recursive Number of levels of association
* @return mixed boolean false on error/failure. An array of results on success.
*/
public function read(Model $model, $queryData = array(), $recursive = null) {
public function read(Model $Model, $queryData = array(), $recursive = null) {
$queryData = $this->_scrubQueryData($queryData);
$null = null;
$array = array('callbacks' => $queryData['callbacks']);
$linkedModels = array();
$bypass = false;
if ($recursive === null && isset($queryData['recursive'])) {
$recursive = $queryData['recursive'];
}
if ($recursive !== null) {
$_recursive = $model->recursive;
$model->recursive = $recursive;
$_recursive = $Model->recursive;
$Model->recursive = $recursive;
}
$bypass = false;
if (!empty($queryData['fields'])) {
$bypass = true;
$queryData['fields'] = $this->fields($model, null, $queryData['fields']);
$queryData['fields'] = $this->fields($Model, null, $queryData['fields']);
} else {
$queryData['fields'] = $this->fields($model);
$queryData['fields'] = $this->fields($Model);
}
$_associations = $model->associations();
$_associations = $Model->associations();
if ($model->recursive == -1) {
if ($Model->recursive == -1) {
$_associations = array();
} elseif ($model->recursive === 0) {
} elseif ($Model->recursive === 0) {
unset($_associations[2], $_associations[3]);
}
// Generate hasOne and belongsTo associations inside $queryData
$linkedModels = array();
foreach ($_associations as $type) {
foreach ($model->{$type} as $assoc => $assocData) {
$linkModel = $model->{$assoc};
$external = isset($assocData['external']);
if ($type !== 'hasOne' && $type !== 'belongsTo') {
continue;
}
foreach ($Model->{$type} as $assoc => $assocData) {
$LinkModel = $Model->{$assoc};
if ($Model->useDbConfig !== $LinkModel->useDbConfig) {
continue;
}
$linkModel->getDataSource();
if ($model->useDbConfig === $linkModel->useDbConfig) {
if ($bypass) {
$assocData['fields'] = false;
}
if ($this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null) === true) {
$external = isset($assocData['external']);
if ($this->generateAssociationQuery($Model, $LinkModel, $type, $assoc, $assocData, $queryData, $external) === true) {
$linkedModels[$type . '/' . $assoc] = true;
}
}
}
}
$query = $this->generateAssociationQuery($model, null, null, null, null, $queryData, false, $null);
// Build SQL statement with the primary model, plus hasOne and belongsTo associations
$query = $this->buildAssociationQuery($Model, $queryData);
$resultSet = $this->fetchAll($query, $model->cacheQueries);
$resultSet = $this->fetchAll($query, $Model->cacheQueries);
unset($query);
if ($resultSet === false) {
$model->onError();
$Model->onError();
return false;
}
$filtered = array();
// Filter hasOne and belongsTo associations
if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
$filtered = $this->_filterResults($resultSet, $model);
$filtered = $this->_filterResults($resultSet, $Model);
}
if ($model->recursive > -1) {
// Deep associations
if ($Model->recursive > -1) {
$joined = array();
if (isset($queryData['joins'][0]['alias'])) {
$joined[$model->alias] = (array)Hash::extract($queryData['joins'], '{n}.alias');
$joined[$Model->alias] = (array)Hash::extract($queryData['joins'], '{n}.alias');
}
foreach ($_associations as $type) {
foreach ($model->{$type} as $assoc => $assocData) {
$linkModel = $model->{$assoc};
foreach ($Model->{$type} as $assoc => $assocData) {
$LinkModel = $Model->{$assoc};
if (!isset($linkedModels[$type . '/' . $assoc])) {
if ($model->useDbConfig === $linkModel->useDbConfig) {
$db = $this;
} else {
$db = ConnectionManager::getDataSource($linkModel->useDbConfig);
}
} elseif ($model->recursive > 1 && ($type === 'belongsTo' || $type === 'hasOne')) {
$db = $Model->useDbConfig === $LinkModel->useDbConfig ? $this : $LinkModel->getDataSource();
} elseif ($Model->recursive > 1) {
$db = $this;
}
if (isset($db) && method_exists($db, 'queryAssociation')) {
$stack = array($assoc);
$stack['_joined'] = $joined;
$db->queryAssociation($model, $linkModel, $type, $assoc, $assocData, $array, true, $resultSet, $model->recursive - 1, $stack);
unset($db);
$db->queryAssociation($Model, $LinkModel, $type, $assoc, $assocData, $array, true, $resultSet, $Model->recursive - 1, $stack);
if ($type === 'hasMany' || $type === 'hasAndBelongsToMany') {
$filtered[] = $assoc;
@ -1117,43 +1133,52 @@ class DboSource extends DataSource {
}
}
}
if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
$this->_filterResults($resultSet, $model, $filtered);
$this->_filterResults($resultSet, $Model, $filtered);
}
}
if ($recursive !== null) {
$model->recursive = $_recursive;
$Model->recursive = $_recursive;
}
return $resultSet;
}
/**
* Passes association results thru afterFind filters of corresponding model
* Passes association results through afterFind filters of corresponding model.
*
* The primary model is always filtered.
*
* @param array $results Reference of resultset to be filtered
* @param Model $model Instance of model to operate against
* @param Model $Model Instance of model to operate against
* @param array $filtered List of classes already filtered, to be skipped
* @return array Array of results that have been filtered through $model->afterFind
* @return array Array of results that have been filtered through $Model->afterFind
*/
protected function _filterResults(&$results, Model $model, $filtered = array()) {
protected function _filterResults(&$results, Model $Model, $filtered = array()) {
if (!is_array($results)) {
return array();
}
$current = reset($results);
if (!is_array($current)) {
return array();
}
$keys = array_diff(array_keys($current), $filtered, array($model->alias));
$keys = array_diff(array_keys($current), $filtered, array($Model->alias));
$filtering = array();
foreach ($keys as $className) {
if (!isset($model->{$className}) || !is_object($model->{$className})) {
if (!isset($Model->{$className}) || !is_object($Model->{$className})) {
continue;
}
$linkedModel = $model->{$className};
$LinkedModel = $Model->{$className};
$filtering[] = $className;
foreach ($results as $key => &$result) {
$data = $linkedModel->afterFind(array(array($className => $result[$className])), false);
$data = $LinkedModel->afterFind(array(array($className => $result[$className])), false);
if (isset($data[0][$className])) {
$result[$className] = $data[0][$className];
} else {
@ -1161,14 +1186,15 @@ class DboSource extends DataSource {
}
}
}
return $filtering;
}
/**
* Queries associations. Used to fetch results on recursive models.
*
* @param Model $model Primary Model object
* @param Model $linkModel Linked model that
* @param Model $Model Primary Model object
* @param Model $LinkModel Linked model that
* @param string $type Association type, one of the model association types ie. hasMany
* @param string $association
* @param array $assocData
@ -1180,58 +1206,62 @@ class DboSource extends DataSource {
* @return mixed
* @throws CakeException when results cannot be created.
*/
public function queryAssociation(Model $model, &$linkModel, $type, $association, $assocData, &$queryData, $external, &$resultSet, $recursive, $stack) {
public function queryAssociation(Model $Model, Model $LinkModel, $type, $association, $assocData, &$queryData, $external, &$resultSet, $recursive, $stack) {
if (isset($stack['_joined'])) {
$joined = $stack['_joined'];
unset($stack['_joined']);
}
if ($query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet)) {
if (!is_array($resultSet)) {
throw new CakeException(__d('cake_dev', 'Error in Model %s', get_class($model)));
$query = $this->generateAssociationQuery($Model, $LinkModel, $type, $association, $assocData, $queryData, $external);
if (empty($query)) {
return;
}
if (!is_array($resultSet)) {
throw new CakeException(__d('cake_dev', 'Error in Model %s', get_class($Model)));
}
if ($type === 'hasMany' && empty($assocData['limit']) && !empty($assocData['foreignKey'])) {
$ins = $fetch = array();
foreach ($resultSet as &$result) {
if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $assocData, $model, $linkModel, $stack)) {
if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $Model, $stack)) {
$ins[] = $in;
}
}
if (!empty($ins)) {
$ins = array_unique($ins);
$fetch = $this->fetchAssociated($model, $query, $ins);
$fetch = $this->fetchAssociated($Model, $query, $ins);
}
if (!empty($fetch) && is_array($fetch)) {
if ($recursive > 0) {
foreach ($linkModel->associations() as $type1) {
foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
$deepModel = $linkModel->{$assoc1};
if ($recursive > 0 && !empty($fetch) && is_array($fetch)) {
foreach ($LinkModel->associations() as $type1) {
foreach ($LinkModel->{$type1} as $assoc1 => $assocData1) {
$DeepModel = $LinkModel->{$assoc1};
$tmpStack = $stack;
$tmpStack[] = $assoc1;
if ($linkModel->useDbConfig === $deepModel->useDbConfig) {
$db = $this;
} else {
$db = ConnectionManager::getDataSource($deepModel->useDbConfig);
}
$db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack);
}
$db = $LinkModel->useDbConfig === $DeepModel->useDbConfig ? $this : $DeepModel->getDataSource();
$db->queryAssociation($LinkModel, $DeepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack);
}
}
}
if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
$this->_filterResults($fetch, $model);
$this->_filterResults($fetch, $Model);
}
return $this->_mergeHasMany($resultSet, $fetch, $association, $model, $linkModel);
return $this->_mergeHasMany($resultSet, $fetch, $association, $Model);
} elseif ($type === 'hasAndBelongsToMany') {
$ins = $fetch = array();
foreach ($resultSet as &$result) {
if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $assocData, $model, $linkModel, $stack)) {
if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $Model, $stack)) {
$ins[] = $in;
}
}
if (!empty($ins)) {
$ins = array_unique($ins);
if (count($ins) > 1) {
@ -1243,66 +1273,69 @@ class DboSource extends DataSource {
$query = str_replace(' WHERE 1 = 1', '', $query);
}
$foreignKey = $model->hasAndBelongsToMany[$association]['foreignKey'];
$joinKeys = array($foreignKey, $model->hasAndBelongsToMany[$association]['associationForeignKey']);
list($with, $habtmFields) = $model->joinModel($model->hasAndBelongsToMany[$association]['with'], $joinKeys);
$foreignKey = $Model->hasAndBelongsToMany[$association]['foreignKey'];
$joinKeys = array($foreignKey, $Model->hasAndBelongsToMany[$association]['associationForeignKey']);
list($with, $habtmFields) = $Model->joinModel($Model->hasAndBelongsToMany[$association]['with'], $joinKeys);
$habtmFieldsCount = count($habtmFields);
$q = $this->insertQueryData($query, null, $association, $assocData, $model, $linkModel, $stack);
$q = $this->insertQueryData($query, null, $association, $Model, $stack);
if ($q !== false) {
$fetch = $this->fetchAll($q, $model->cacheQueries);
$fetch = $this->fetchAll($q, $Model->cacheQueries);
} else {
$fetch = null;
}
if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
$this->_filterResults($fetch, $model);
$this->_filterResults($fetch, $Model);
}
}
$modelAlias = $model->alias;
$modelPK = $model->primaryKey;
$modelAlias = $Model->alias;
$primaryKey = $Model->primaryKey;
foreach ($resultSet as &$row) {
if ($type !== 'hasAndBelongsToMany') {
$q = $this->insertQueryData($query, $row, $association, $assocData, $model, $linkModel, $stack);
$q = $this->insertQueryData($query, $row, $association, $Model, $stack);
$fetch = null;
if ($q !== false) {
$joinedData = array();
if (($type === 'belongsTo' || $type === 'hasOne') && isset($row[$linkModel->alias], $joined[$model->alias]) && in_array($linkModel->alias, $joined[$model->alias])) {
$joinedData = Hash::filter($row[$linkModel->alias]);
if (($type === 'belongsTo' || $type === 'hasOne') && isset($row[$LinkModel->alias], $joined[$Model->alias]) && in_array($LinkModel->alias, $joined[$Model->alias])) {
$joinedData = Hash::filter($row[$LinkModel->alias]);
if (!empty($joinedData)) {
$fetch[0] = array($linkModel->alias => $row[$linkModel->alias]);
$fetch[0] = array($LinkModel->alias => $row[$LinkModel->alias]);
}
} else {
$fetch = $this->fetchAll($q, $model->cacheQueries);
$fetch = $this->fetchAll($q, $Model->cacheQueries);
}
}
}
$selfJoin = $linkModel->name === $model->name;
$selfJoin = ($Model->name === $LinkModel->name);
if (!empty($fetch) && is_array($fetch)) {
if ($recursive > 0) {
foreach ($linkModel->associations() as $type1) {
foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
$deepModel = $linkModel->{$assoc1};
foreach ($LinkModel->associations() as $type1) {
foreach ($LinkModel->{$type1} as $assoc1 => $assocData1) {
$DeepModel = $LinkModel->{$assoc1};
if ($type1 === 'belongsTo' || ($deepModel->alias === $modelAlias && $type === 'belongsTo') || ($deepModel->alias !== $modelAlias)) {
if ($type1 === 'belongsTo' || ($DeepModel->alias === $modelAlias && $type === 'belongsTo') || ($DeepModel->alias !== $modelAlias)) {
$tmpStack = $stack;
$tmpStack[] = $assoc1;
if ($linkModel->useDbConfig == $deepModel->useDbConfig) {
$db = $this;
} else {
$db = ConnectionManager::getDataSource($deepModel->useDbConfig);
}
$db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack);
$db = $LinkModel->useDbConfig === $DeepModel->useDbConfig ? $this : $DeepModel->getDataSource();
$db->queryAssociation($LinkModel, $DeepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack);
}
}
}
}
if ($type === 'hasAndBelongsToMany') {
$merge = array();
foreach ($fetch as $data) {
if (isset($data[$with]) && $data[$with][$foreignKey] === $row[$modelAlias][$modelPK]) {
if (isset($data[$with]) && $data[$with][$foreignKey] === $row[$modelAlias][$primaryKey]) {
if ($habtmFieldsCount <= 2) {
unset($data[$with]);
}
@ -1317,31 +1350,32 @@ class DboSource extends DataSource {
} else {
$this->_mergeAssociation($row, $fetch, $association, $type, $selfJoin);
}
if (isset($row[$association]) && $type !== 'hasAndBelongsToMany') {
$row[$association] = $linkModel->afterFind($row[$association], false);
if ($type !== 'hasAndBelongsToMany' && isset($row[$association])) {
$row[$association] = $LinkModel->afterFind($row[$association], false);
}
} else {
$tempArray[0][$association] = false;
$this->_mergeAssociation($row, $tempArray, $association, $type, $selfJoin);
}
}
}
}
/**
* A more efficient way to fetch associations.
*
* @param Model $model Primary model object
* @param Model $Model Primary model object
* @param string $query Association query
* @param array $ids Array of IDs of associated records
* @return array Association results
*/
public function fetchAssociated(Model $model, $query, $ids) {
public function fetchAssociated(Model $Model, $query, $ids) {
$query = str_replace('{$__cakeID__$}', implode(', ', $ids), $query);
if (count($ids) > 1) {
$query = str_replace('= (', 'IN (', $query);
}
return $this->fetchAll($query, $model->cacheQueries);
return $this->fetchAll($query, $Model->cacheQueries);
}
/**
@ -1350,21 +1384,27 @@ class DboSource extends DataSource {
* @param array $resultSet Data to merge into
* @param array $merge Data to merge
* @param string $association Name of Model being Merged
* @param Model $model Model being merged onto
* @param Model $linkModel Model being merged
* @param Model $Model Model being merged onto
* @return void
*/
protected function _mergeHasMany(&$resultSet, $merge, $association, $model, $linkModel) {
$modelAlias = $model->alias;
$modelPK = $model->primaryKey;
$modelFK = $model->hasMany[$association]['foreignKey'];
protected function _mergeHasMany(&$resultSet, $merge, $association, $Model) {
$modelAlias = $Model->alias;
$primaryKey = $Model->primaryKey;
$foreignKey = $Model->hasMany[$association]['foreignKey'];
foreach ($resultSet as &$result) {
if (!isset($result[$modelAlias])) {
continue;
}
$resultPrimaryKey = $result[$modelAlias][$primaryKey];
$merged = array();
foreach ($merge as $data) {
if ($result[$modelAlias][$modelPK] === $data[$association][$modelFK]) {
if ($resultPrimaryKey !== $data[$association][$foreignKey]) {
continue;
}
if (count($data) > 1) {
$data = array_merge($data[$association], $data);
unset($data[$association]);
@ -1379,7 +1419,7 @@ class DboSource extends DataSource {
$merged[] = $data[$association];
}
}
}
$result = Hash::mergeDiff($result, array($association => $merged));
}
}
@ -1399,9 +1439,11 @@ class DboSource extends DataSource {
$association = Inflector::pluralize($association);
}
$dataAssociation =& $data[$association];
if ($type === 'belongsTo' || $type === 'hasOne') {
if (isset($merge[$association])) {
$data[$association] = $merge[$association][0];
$dataAssociation = $merge[$association][0];
} else {
if (!empty($merge[0][$association])) {
foreach ($merge[0] as $assoc => $data2) {
@ -1410,14 +1452,14 @@ class DboSource extends DataSource {
}
}
}
if (!isset($data[$association])) {
$data[$association] = array();
if (!isset($dataAssociation)) {
$dataAssociation = array();
if ($merge[0][$association]) {
$data[$association] = $merge[0][$association];
$dataAssociation = $merge[0][$association];
}
} else {
if (is_array($merge[0][$association])) {
foreach ($data[$association] as $k => $v) {
foreach ($dataAssociation as $k => $v) {
if (!is_array($v)) {
$dataAssocTmp[$k] = $v;
}
@ -1432,20 +1474,20 @@ class DboSource extends DataSource {
$mergeKeys = array_keys($merge[0]);
if ($mergeKeys[0] === $dataKeys[0] || $mergeKeys === $dataKeys) {
$data[$association][$association] = $merge[0][$association];
$dataAssociation[$association] = $merge[0][$association];
} else {
$diff = Hash::diff($dataAssocTmp, $mergeAssocTmp);
$data[$association] = array_merge($merge[0][$association], $diff);
$dataAssociation = array_merge($merge[0][$association], $diff);
}
} elseif ($selfJoin && array_key_exists($association, $merge[0])) {
$data[$association] = array_merge($data[$association], array($association => array()));
$dataAssociation = array_merge($dataAssociation, array($association => array()));
}
}
}
} else {
if (isset($merge[0][$association]) && $merge[0][$association] === false) {
if (!isset($data[$association])) {
$data[$association] = array();
if (!isset($dataAssociation)) {
$dataAssociation = array();
}
} else {
foreach ($merge as $row) {
@ -1457,8 +1499,8 @@ class DboSource extends DataSource {
unset($insert[$association]);
}
if (empty($data[$association]) || (isset($data[$association]) && !in_array($insert, $data[$association], true))) {
$data[$association][] = $insert;
if (empty($dataAssociation) || (isset($dataAssociation) && !in_array($insert, $dataAssociation, true))) {
$dataAssociation[] = $insert;
}
}
}
@ -1466,46 +1508,52 @@ class DboSource extends DataSource {
}
/**
* Generates an array representing a query or part of a query from a single model or two associated models
* Prepares fields required by an SQL statement.
*
* @param Model $model
* @param Model $linkModel
* @param string $type
* @param string $association
* @param array $assocData
* When no fields are set, all the $Model fields are returned.
*
* @param Model $Model
* @param array $queryData
* @param boolean $external
* @param array $resultSet
* @return mixed
* @return array Array containing SQL fields.
*/
public function generateAssociationQuery(Model $model, $linkModel, $type, $association, $assocData, &$queryData, $external, &$resultSet) {
$queryData = $this->_scrubQueryData($queryData);
$assocData = $this->_scrubQueryData($assocData);
$modelAlias = $model->alias;
public function prepareFields(Model $Model, $queryData) {
if (empty($queryData['fields'])) {
$queryData['fields'] = $this->fields($model, $modelAlias);
} elseif (!empty($model->hasMany) && $model->recursive > -1) {
$assocFields = $this->fields($model, $modelAlias, array("{$modelAlias}.{$model->primaryKey}"));
$queryData['fields'] = $this->fields($Model);
} elseif (!empty($Model->hasMany) && $Model->recursive > -1) {
// hasMany relationships need the $Model primary key.
$assocFields = $this->fields($Model, null, "{$Model->alias}.{$Model->primaryKey}");
$passedFields = $queryData['fields'];
if (count($passedFields) === 1) {
if (strpos($passedFields[0], $assocFields[0]) === false && !preg_match('/^[a-z]+\(/i', $passedFields[0])) {
$queryData['fields'] = array_merge($passedFields, $assocFields);
} else {
$queryData['fields'] = $passedFields;
}
} else {
if (
count($passedFields) > 1 ||
(strpos($passedFields[0], $assocFields[0]) === false && !preg_match('/^[a-z]+\(/i', $passedFields[0]))
) {
$queryData['fields'] = array_merge($passedFields, $assocFields);
}
unset($assocFields, $passedFields);
}
if ($linkModel === null) {
return array_unique($queryData['fields']);
}
/**
* Builds an SQL statement.
*
* This is merely a convenient wrapper to DboSource::buildStatement().
*
* @param Model $Model
* @param array $queryData
* @return string String containing an SQL statement.
* @see DboSource::buildStatement()
*/
public function buildAssociationQuery(Model $Model, $queryData) {
$queryData = $this->_scrubQueryData($queryData);
return $this->buildStatement(
array(
'fields' => array_unique($queryData['fields']),
'table' => $this->fullTableName($model),
'alias' => $modelAlias,
'fields' => $this->prepareFields($Model, $queryData),
'table' => $this->fullTableName($Model),
'alias' => $Model->alias,
'limit' => $queryData['limit'],
'offset' => $queryData['offset'],
'joins' => $queryData['joins'],
@ -1513,32 +1561,48 @@ class DboSource extends DataSource {
'order' => $queryData['order'],
'group' => $queryData['group']
),
$model
$Model
);
}
/**
* Generates an array representing a query or part of a query from a single model or two associated models.
*
* @param Model $Model
* @param Model $LinkModel
* @param string $type Association type, one of the model association types ie. hasMany
* @param string $association
* @param array $assocData
* @param array $queryData
* @param boolean $external Whether or not the association query is on an external datasource.
* @return mixed
* True. when $external is false and association $type is 'hasOne' or 'belongsTo'.
*/
public function generateAssociationQuery(Model $Model, Model $LinkModel, $type, $association, $assocData, &$queryData, $external) {
$assocData = $this->_scrubQueryData($assocData);
if ($external && !empty($assocData['finderQuery'])) {
return $assocData['finderQuery'];
}
$self = $model->name === $linkModel->name;
$fields = array();
if ($external || (in_array($type, array('hasOne', 'belongsTo')) && $assocData['fields'] !== false)) {
$fields = $this->fields($linkModel, $association, $assocData['fields']);
}
if (in_array($type, array('hasMany', 'hasAndBelongsToMany'))) {
if (empty($assocData['offset']) && !empty($assocData['page'])) {
$assocData['offset'] = ($assocData['page'] - 1) * $assocData['limit'];
}
}
switch ($type) {
case 'hasOne':
case 'belongsTo':
$conditions = $this->_mergeConditions(
$assocData['conditions'],
$this->getConstraint($type, $model, $linkModel, $association, array_merge($assocData, compact('external', 'self')))
$this->getConstraint($type, $Model, $LinkModel, $association, array_merge($assocData, compact('external')))
);
if (!$self && $external) {
$selfJoin = ($Model->name === $LinkModel->name);
if ($external && !$selfJoin) {
$modelAlias = $Model->alias;
foreach ($conditions as $key => $condition) {
if (is_numeric($key) && strpos($condition, $modelAlias . '.') !== false) {
unset($conditions[$key]);
@ -1549,38 +1613,49 @@ class DboSource extends DataSource {
if ($external) {
$query = array_merge($assocData, array(
'conditions' => $conditions,
'table' => $this->fullTableName($linkModel),
'fields' => $fields,
'table' => $this->fullTableName($LinkModel),
'fields' => $this->fields($LinkModel, $association, $assocData['fields']),
'alias' => $association,
'group' => null
));
} else {
$join = array(
'table' => $linkModel,
'table' => $LinkModel,
'alias' => $association,
'type' => isset($assocData['type']) ? $assocData['type'] : 'LEFT',
'conditions' => trim($this->conditions($conditions, true, false, $model))
'conditions' => trim($this->conditions($conditions, true, false, $Model))
);
$queryData['fields'] = array_merge($queryData['fields'], $fields);
$queryData = $this->_scrubQueryData($queryData);
$fields = array();
if ($assocData['fields'] !== false) {
$fields = $this->fields($LinkModel, $association, $assocData['fields']);
}
$queryData['fields'] = array_merge($this->prepareFields($Model, $queryData), $fields);
if (!empty($assocData['order'])) {
$queryData['order'][] = $assocData['order'];
}
if (!in_array($join, $queryData['joins'])) {
$queryData['joins'][] = $join;
}
return true;
}
break;
case 'hasMany':
$assocData['fields'] = $this->fields($linkModel, $association, $assocData['fields']);
$assocData['fields'] = $this->fields($LinkModel, $association, $assocData['fields']);
if (!empty($assocData['foreignKey'])) {
$assocData['fields'] = array_merge($assocData['fields'], $this->fields($linkModel, $association, array("{$association}.{$assocData['foreignKey']}")));
$assocData['fields'] = array_merge($assocData['fields'], $this->fields($LinkModel, $association, array("{$association}.{$assocData['foreignKey']}")));
}
$query = array(
'conditions' => $this->_mergeConditions($this->getConstraint('hasMany', $model, $linkModel, $association, $assocData), $assocData['conditions']),
'conditions' => $this->_mergeConditions($this->getConstraint('hasMany', $Model, $LinkModel, $association, $assocData), $assocData['conditions']),
'fields' => array_unique($assocData['fields']),
'table' => $this->fullTableName($linkModel),
'table' => $this->fullTableName($LinkModel),
'alias' => $association,
'order' => $assocData['order'],
'limit' => $assocData['limit'],
@ -1594,14 +1669,14 @@ class DboSource extends DataSource {
if (isset($assocData['with']) && !empty($assocData['with'])) {
$joinKeys = array($assocData['foreignKey'], $assocData['associationForeignKey']);
list($with, $joinFields) = $model->joinModel($assocData['with'], $joinKeys);
list($with, $joinFields) = $Model->joinModel($assocData['with'], $joinKeys);
$joinTbl = $model->{$with};
$joinTbl = $Model->{$with};
$joinAlias = $joinTbl;
if (is_array($joinFields) && !empty($joinFields)) {
$joinAssoc = $joinAlias = $model->{$with}->alias;
$joinFields = $this->fields($model->{$with}, $joinAlias, $joinFields);
$joinAssoc = $joinAlias = $joinTbl->alias;
$joinFields = $this->fields($joinTbl, $joinAlias, $joinFields);
} else {
$joinFields = array();
}
@ -1609,26 +1684,29 @@ class DboSource extends DataSource {
$joinTbl = $assocData['joinTable'];
$joinAlias = $this->fullTableName($assocData['joinTable']);
}
$query = array(
'conditions' => $assocData['conditions'],
'limit' => $assocData['limit'],
'offset' => $assocData['offset'],
'table' => $this->fullTableName($linkModel),
'table' => $this->fullTableName($LinkModel),
'alias' => $association,
'fields' => array_merge($this->fields($linkModel, $association, $assocData['fields']), $joinFields),
'fields' => array_merge($this->fields($LinkModel, $association, $assocData['fields']), $joinFields),
'order' => $assocData['order'],
'group' => null,
'joins' => array(array(
'table' => $joinTbl,
'alias' => $joinAssoc,
'conditions' => $this->getConstraint('hasAndBelongsToMany', $model, $linkModel, $joinAlias, $assocData, $association)
'conditions' => $this->getConstraint('hasAndBelongsToMany', $Model, $LinkModel, $joinAlias, $assocData, $association)
))
);
break;
}
if (isset($query)) {
return $this->buildStatement($query, $model);
return $this->buildStatement($query, $Model);
}
return null;
}
@ -1644,7 +1722,7 @@ class DboSource extends DataSource {
* @return array Conditions array defining the constraint between $model and $association
*/
public function getConstraint($type, $model, $linkModel, $alias, $assoc, $alias2 = null) {
$assoc += array('external' => false, 'self' => false);
$assoc += array('external' => false);
if (empty($assoc['foreignKey'])) {
return array();
@ -1708,6 +1786,7 @@ class DboSource extends DataSource {
*/
public function buildStatement($query, $model) {
$query = array_merge($this->_queryDefaults, $query);
if (!empty($query['joins'])) {
$count = count($query['joins']);
for ($i = 0; $i < $count; $i++) {
@ -1716,6 +1795,7 @@ class DboSource extends DataSource {
}
}
}
return $this->renderStatement('select', array(
'conditions' => $this->conditions($query['conditions'], true, true, $model),
'fields' => implode(', ', $query['fields']),
@ -1970,27 +2050,39 @@ class DboSource extends DataSource {
/**
* Returns an array of SQL JOIN fragments from a model's associations
*
* @param Model $model
* @param Model $Model
* @return array
*/
protected function _getJoins(Model $model) {
protected function _getJoins(Model $Model) {
$join = array();
$joins = array_merge($model->getAssociated('hasOne'), $model->getAssociated('belongsTo'));
$joins = array_merge($Model->getAssociated('hasOne'), $Model->getAssociated('belongsTo'));
foreach ($joins as $assoc) {
if (isset($model->{$assoc}) && $model->useDbConfig == $model->{$assoc}->useDbConfig && $model->{$assoc}->getDataSource()) {
$assocData = $model->getAssociated($assoc);
if (!isset($Model->{$assoc})) {
continue;
}
$LinkModel = $Model->{$assoc};
if ($Model->useDbConfig !== $LinkModel->useDbConfig) {
continue;
}
$assocData = $Model->getAssociated($assoc);
$join[] = $this->buildJoinStatement(array(
'table' => $model->{$assoc},
'table' => $LinkModel,
'alias' => $assoc,
'type' => isset($assocData['type']) ? $assocData['type'] : 'LEFT',
'conditions' => trim($this->conditions(
$this->_mergeConditions($assocData['conditions'], $this->getConstraint($assocData['association'], $model, $model->{$assoc}, $assoc, $assocData)),
true, false, $model
$this->_mergeConditions($assocData['conditions'], $this->getConstraint($assocData['association'], $Model, $LinkModel, $assoc, $assocData)),
true,
false,
$Model
))
));
}
}
return $join;
}

View file

@ -1142,18 +1142,18 @@ class MysqlTest extends CakeTestCase {
$external = isset($assocData['external']);
if ($this->Model->Category2->alias == $linkModel->alias && $type !== 'hasAndBelongsToMany' && $type !== 'hasMany') {
$result = $this->Dbo->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null);
$result = $this->Dbo->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external);
$this->assertFalse(empty($result));
} else {
if ($this->Model->Category2->useDbConfig == $linkModel->useDbConfig) {
$result = $this->Dbo->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null);
$result = $this->Dbo->generateAssociationQuery($this->Model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external);
$this->assertFalse(empty($result));
}
}
}
}
$query = $this->Dbo->generateAssociationQuery($this->Model->Category2, $null, null, null, null, $queryData, false, $null);
$query = $this->Dbo->buildAssociationQuery($this->Model->Category2, $queryData);
$this->assertRegExp('/^SELECT\s+(.+)FROM(.+)`Category2`\.`group_id`\s+=\s+`Group`\.`id`\)\s+LEFT JOIN(.+)WHERE\s+1 = 1\s*$/', $query);
$this->Model = new TestModel4();
@ -1162,13 +1162,11 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$_queryData = $queryData;
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertTrue($result);
$expected = array(
@ -1200,7 +1198,7 @@ class MysqlTest extends CakeTestCase {
$queryData['joins'][0]['table'] = $this->Dbo->fullTableName($queryData['joins'][0]['table']);
$this->assertEquals($expected, $queryData);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/', $result);
$this->assertRegExp('/FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4Parent`/', $result);
$this->assertRegExp('/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/', $result);
@ -1208,7 +1206,7 @@ class MysqlTest extends CakeTestCase {
$params['assocData']['type'] = 'INNER';
$this->Model->belongsTo['TestModel4Parent']['type'] = 'INNER';
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $_queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $_queryData, $params['external']);
$this->assertTrue($result);
$this->assertEquals('INNER', $_queryData['joins'][0]['type']);
}
@ -1315,14 +1313,12 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasOne', 'model' => 'TestModel9');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertTrue($result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`, `TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`\s+/', $result);
$this->assertRegExp('/FROM\s+\S+`test_model8` AS `TestModel8`\s+LEFT JOIN\s+\S+`test_model9` AS `TestModel9`/', $result);
$this->assertRegExp('/\s+ON\s+\(`TestModel9`\.`name` != \'mariano\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/', $result);
@ -1341,14 +1337,12 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'belongsTo', 'model' => 'TestModel8');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertTrue($result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`, `TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`\s+/', $result);
$this->assertRegExp('/FROM\s+\S+`test_model9` AS `TestModel9`\s+LEFT JOIN\s+\S+`test_model8` AS `TestModel8`/', $result);
$this->assertRegExp('/\s+ON\s+\(`TestModel8`\.`name` != \'larry\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/', $result);
@ -1367,15 +1361,13 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent');
$queryData = array('conditions' => array('TestModel4Parent.name !=' => 'mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertTrue($result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/', $result);
$this->assertRegExp('/FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4Parent`/', $result);
$this->assertRegExp('/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/', $result);
@ -1397,14 +1389,12 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'belongsTo', 'model' => 'ArticleFeatured2');
$queryData = array('conditions' => array());
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Featured2, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Featured2, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Featured2, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertTrue($result);
$result = $this->Dbo->generateAssociationQuery($this->Featured2, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Featured2, $queryData);
$this->assertRegExp(
'/^SELECT\s+`Featured2`\.`id`, `Featured2`\.`article_id`, `Featured2`\.`category_id`, `Featured2`\.`name`,\s+' .
@ -1429,12 +1419,10 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasOne', 'model' => 'TestModel5');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertTrue($result);
$testModel5Table = $this->Dbo->fullTableName($this->Model->TestModel5);
@ -1442,7 +1430,7 @@ class MysqlTest extends CakeTestCase {
$expected = ' LEFT JOIN ' . $testModel5Table . ' AS `TestModel5` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
$this->assertEquals(trim($expected), trim($result));
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+/', $result);
$this->assertRegExp('/`test_model5` AS `TestModel5`\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE/', $result);
@ -1462,15 +1450,13 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasOne', 'model' => 'TestModel5');
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertTrue($result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+\S+`test_model5` AS `TestModel5`/', $result);
@ -1490,12 +1476,10 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'belongsTo', 'model' => 'TestModel4');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertTrue($result);
$testModel4Table = $this->Dbo->fullTableName($this->Model->TestModel4, true, true);
@ -1503,7 +1487,7 @@ class MysqlTest extends CakeTestCase {
$expected = ' LEFT JOIN ' . $testModel4Table . ' AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
$this->assertEquals(trim($expected), trim($result));
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4`/', $result);
$this->assertRegExp('/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/', $result);
@ -1522,12 +1506,10 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'belongsTo', 'model' => 'TestModel4');
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertTrue($result);
$testModel4Table = $this->Dbo->fullTableName($this->Model->TestModel4, true, true);
@ -1535,7 +1517,7 @@ class MysqlTest extends CakeTestCase {
$expected = ' LEFT JOIN ' . $testModel4Table . ' AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
$this->assertEquals(trim($expected), trim($result));
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4`/', $result);
$this->assertRegExp('/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/', $result);
@ -1554,18 +1536,16 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE/', $result);
$this->assertRegExp('/\s+WHERE\s+`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/', $result);
@ -1585,12 +1565,10 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp(
'/^SELECT\s+' .
'`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+' .
@ -1600,7 +1578,7 @@ class MysqlTest extends CakeTestCase {
'\s*$/', $result
);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp(
'/^SELECT\s+' .
'`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+' .
@ -1622,17 +1600,15 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
$this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?`TestModel5`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result);
@ -1655,19 +1631,17 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
$this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$this->assertRegExp('/\s+LIMIT 2,\s*5\s*$/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
@ -1691,19 +1665,16 @@ class MysqlTest extends CakeTestCase {
$this->Model->hasMany['TestModel6']['limit'] = 5;
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
$this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$this->assertRegExp('/\s+LIMIT 5,\s*5\s*$/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
@ -1723,51 +1694,45 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`name`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
$this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`id`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`id`, `TestModel5`.`name`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
$this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`name`', '`TestModel5`.`created`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
$this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`id`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
@ -1776,17 +1741,15 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
$this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
@ -1797,17 +1760,15 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
$this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
@ -1818,17 +1779,15 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel6`\.`test_model5_id`, `TestModel6`\.`name`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/', $result);
$this->assertRegExp('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
@ -1848,13 +1807,10 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('MIN(`TestModel5`.`test_model4_id`)'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$this->Model->recursive = 0;
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, $params['type'], $params['assoc'], $params['assocData'], $queryData, false, $resultSet);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+MIN\(`TestModel5`\.`test_model4_id`\)\s+FROM/', $result);
}
@ -1870,12 +1826,10 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
$queryData = array();
$resultSet = null;
$null = null;
$params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$assocTable = $this->Dbo->fullTableName($this->Model->TestModel4TestModel7, true, true);
$this->assertRegExp('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model7` AS `TestModel7`\s+JOIN\s+' . $assocTable . '/', $result);
@ -1883,7 +1837,7 @@ class MysqlTest extends CakeTestCase {
$this->assertRegExp('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)/', $result);
$this->assertRegExp('/WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result);
@ -1901,18 +1855,16 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
$queryData = array('conditions' => array('TestModel4.name !=' => 'mariano'));
$resultSet = null;
$null = null;
$params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+\S+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result);
$this->assertRegExp('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/', $result);
$this->assertRegExp('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?`TestModel4`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/', $result);
}
@ -1934,19 +1886,17 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+\S+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result);
$this->assertRegExp('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\s+/', $result);
$this->assertRegExp('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 2,\s*5\s*$/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
@ -1970,19 +1920,17 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$result = $this->Dbo->generateAssociationQuery($this->Model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external']);
$this->assertRegExp('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+\S+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/', $result);
$this->assertRegExp('/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/', $result);
$this->assertRegExp('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 5,\s*5\s*$/', $result);
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null);
$result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
$this->assertRegExp('/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
@ -2031,8 +1979,9 @@ class MysqlTest extends CakeTestCase {
$expected = " WHERE SUM(`Post`.`comments_count`) > 500";
$this->assertEquals($expected, $result);
$result = $this->Dbo->conditions("(Post.created < '" . date('Y-m-d H:i') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
$expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
$date = date('Y-m-d H:i');
$result = $this->Dbo->conditions("(Post.created < '" . $date . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
$expected = " WHERE (`Post`.`created` < '" . $date . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
$this->assertEquals($expected, $result);
$result = $this->Dbo->conditions("score BETWEEN 90.1 AND 95.7");
@ -2047,8 +1996,9 @@ class MysqlTest extends CakeTestCase {
$expected = " WHERE `Aro`.`rght` = `Aro`.`lft` + 1.1";
$this->assertEquals($expected, $result);
$result = $this->Dbo->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
$expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
$date = date('Y-m-d H:i:s');
$result = $this->Dbo->conditions("(Post.created < '" . $date . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
$expected = " WHERE (`Post`.`created` < '" . $date . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
$this->assertEquals($expected, $result);
$result = $this->Dbo->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2');