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

View file

@ -1142,18 +1142,18 @@ class MysqlTest extends CakeTestCase {
$external = isset($assocData['external']); $external = isset($assocData['external']);
if ($this->Model->Category2->alias == $linkModel->alias && $type !== 'hasAndBelongsToMany' && $type !== 'hasMany') { 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)); $this->assertFalse(empty($result));
} else { } else {
if ($this->Model->Category2->useDbConfig == $linkModel->useDbConfig) { 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)); $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->assertRegExp('/^SELECT\s+(.+)FROM(.+)`Category2`\.`group_id`\s+=\s+`Group`\.`id`\)\s+LEFT JOIN(.+)WHERE\s+1 = 1\s*$/', $query);
$this->Model = new TestModel4(); $this->Model = new TestModel4();
@ -1162,13 +1162,11 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent'); $binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent');
$queryData = array(); $queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$_queryData = $queryData; $_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); $this->assertTrue($result);
$expected = array( $expected = array(
@ -1200,7 +1198,7 @@ class MysqlTest extends CakeTestCase {
$queryData['joins'][0]['table'] = $this->Dbo->fullTableName($queryData['joins'][0]['table']); $queryData['joins'][0]['table'] = $this->Dbo->fullTableName($queryData['joins'][0]['table']);
$this->assertEquals($expected, $queryData); $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('/^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('/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); $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'; $params['assocData']['type'] = 'INNER';
$this->Model->belongsTo['TestModel4Parent']['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->assertTrue($result);
$this->assertEquals('INNER', $_queryData['joins'][0]['type']); $this->assertEquals('INNER', $_queryData['joins'][0]['type']);
} }
@ -1315,14 +1313,12 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasOne', 'model' => 'TestModel9'); $binding = array('type' => 'hasOne', 'model' => 'TestModel9');
$queryData = array(); $queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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); $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('/^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('/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); $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'); $binding = array('type' => 'belongsTo', 'model' => 'TestModel8');
$queryData = array(); $queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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); $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('/^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('/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); $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'); $binding = array('type' => 'belongsTo', 'model' => 'TestModel4Parent');
$queryData = array('conditions' => array('TestModel4Parent.name !=' => 'mariano')); $queryData = array('conditions' => array('TestModel4Parent.name !=' => 'mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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); $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('/^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('/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); $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'); $binding = array('type' => 'belongsTo', 'model' => 'ArticleFeatured2');
$queryData = array('conditions' => array()); $queryData = array('conditions' => array());
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Featured2, $queryData, $binding); $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); $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( $this->assertRegExp(
'/^SELECT\s+`Featured2`\.`id`, `Featured2`\.`article_id`, `Featured2`\.`category_id`, `Featured2`\.`name`,\s+' . '/^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'); $binding = array('type' => 'hasOne', 'model' => 'TestModel5');
$queryData = array(); $queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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); $this->assertTrue($result);
$testModel5Table = $this->Dbo->fullTableName($this->Model->TestModel5); $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`)'; $expected = ' LEFT JOIN ' . $testModel5Table . ' AS `TestModel5` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
$this->assertEquals(trim($expected), trim($result)); $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('/^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('/\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); $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'); $binding = array('type' => 'hasOne', 'model' => 'TestModel5');
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano')); $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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); $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('/^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); $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'); $binding = array('type' => 'belongsTo', 'model' => 'TestModel4');
$queryData = array(); $queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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); $this->assertTrue($result);
$testModel4Table = $this->Dbo->fullTableName($this->Model->TestModel4, true, true); $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`)'; $expected = ' LEFT JOIN ' . $testModel4Table . ' AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
$this->assertEquals(trim($expected), trim($result)); $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('/^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+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); $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'); $binding = array('type' => 'belongsTo', 'model' => 'TestModel4');
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano')); $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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); $this->assertTrue($result);
$testModel4Table = $this->Dbo->fullTableName($this->Model->TestModel4, true, true); $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`)'; $expected = ' LEFT JOIN ' . $testModel4Table . ' AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)';
$this->assertEquals(trim($expected), trim($result)); $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('/^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+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); $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'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array(); $queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE/', $result);
$this->assertRegExp('/\s+WHERE\s+`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)/', $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('/^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+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\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'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array(); $queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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( $this->assertRegExp(
'/^SELECT\s+' . '/^SELECT\s+' .
'`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+' . '`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+' .
@ -1600,7 +1578,7 @@ class MysqlTest extends CakeTestCase {
'\s*$/', $result '\s*$/', $result
); );
$result = $this->Dbo->generateAssociationQuery($this->Model, $null, null, null, null, $queryData, false, $null); $result = $this->Dbo->buildAssociationQuery($this->Model, $queryData);
$this->assertRegExp( $this->assertRegExp(
'/^SELECT\s+' . '/^SELECT\s+' .
'`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\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'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('conditions' => array('TestModel5.name !=' => 'mariano')); $queryData = array('conditions' => array('TestModel5.name !=' => 'mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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('/\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('/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('/^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+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?`TestModel5`.`name`\s+!=\s+\'mariano\'(?:\))?\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'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array(); $queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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('/\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('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$this->assertRegExp('/\s+LIMIT 2,\s*5\s*$/', $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('/^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+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\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; $this->Model->hasMany['TestModel6']['limit'] = 5;
$binding = array('type' => 'hasMany', 'model' => 'TestModel6'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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('/\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('/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/', $result);
$this->assertRegExp('/\s+LIMIT 5,\s*5\s*$/', $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('/^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+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\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'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`name`')); $queryData = array('fields' => array('`TestModel5`.`name`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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('/\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('/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('/^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+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
$binding = array('type' => 'hasMany', 'model' => 'TestModel6'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`id`, `TestModel5`.`name`')); $queryData = array('fields' => array('`TestModel5`.`id`, `TestModel5`.`name`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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('/\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('/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('/^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+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result); $this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/', $result);
$binding = array('type' => 'hasMany', 'model' => 'TestModel6'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`name`', '`TestModel5`.`created`')); $queryData = array('fields' => array('`TestModel5`.`name`', '`TestModel5`.`created`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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('/\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('/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('/^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+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\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'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`')); $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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('/\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('/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('/^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+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\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'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`')); $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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('/\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('/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('/^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+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\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'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`')); $queryData = array('fields' => array('`TestModel5`.`id`', '`TestModel5`.`name`'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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('/\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('/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('/^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+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\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'); $binding = array('type' => 'hasMany', 'model' => 'TestModel6');
$queryData = array('fields' => array('MIN(`TestModel5`.`test_model4_id`)')); $queryData = array('fields' => array('MIN(`TestModel5`.`test_model4_id`)'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding);
$this->Model->recursive = 0; $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); $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'); $binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
$queryData = array(); $queryData = array();
$resultSet = null;
$null = null;
$params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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); $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('/^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); $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('/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)/', $result);
$this->assertRegExp('/WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $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('/^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+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE/', $result);
$this->assertRegExp('/\s+WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result); $this->assertRegExp('/\s+WHERE\s+(?:\()?1 = 1(?:\))?\s*$/', $result);
@ -1901,18 +1855,16 @@ class MysqlTest extends CakeTestCase {
$binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7'); $binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
$queryData = array('conditions' => array('TestModel4.name !=' => 'mariano')); $queryData = array('conditions' => array('TestModel4.name !=' => 'mariano'));
$resultSet = null;
$null = null;
$params = $this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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+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+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+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('/^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); $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'); $binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
$queryData = array(); $queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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+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+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+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); $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('/^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); $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'); $binding = array('type' => 'hasAndBelongsToMany', 'model' => 'TestModel7');
$queryData = array(); $queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->Model, $queryData, $binding); $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('/^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+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+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+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); $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('/^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); $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"; $expected = " WHERE SUM(`Post`.`comments_count`) > 500";
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Dbo->conditions("(Post.created < '" . date('Y-m-d H:i') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)"); $date = date('Y-m-d H:i');
$expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)"; $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); $this->assertEquals($expected, $result);
$result = $this->Dbo->conditions("score BETWEEN 90.1 AND 95.7"); $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"; $expected = " WHERE `Aro`.`rght` = `Aro`.`lft` + 1.1";
$this->assertEquals($expected, $result); $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)"); $date = date('Y-m-d H:i:s');
$expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)"; $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); $this->assertEquals($expected, $result);
$result = $this->Dbo->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2'); $result = $this->Dbo->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2');