From 0e74a0757a8e1b1efd102cc763395bc3982bd75d Mon Sep 17 00:00:00 2001 From: Ber Clausen Date: Tue, 12 Nov 2013 03:31:08 -0300 Subject: [PATCH] Keep insertQueryData() DRY, issue less lookups. Remove unused $assocData and $LinkModel from the signature. Model objects should be CamelCase. --- lib/Cake/Model/Datasource/DataSource.php | 153 +++++++++++------------ lib/Cake/Model/Datasource/DboSource.php | 10 +- 2 files changed, 80 insertions(+), 83 deletions(-) diff --git a/lib/Cake/Model/Datasource/DataSource.php b/lib/Cake/Model/Datasource/DataSource.php index ceda8a4c8..f71b40f53 100644 --- a/lib/Cake/Model/Datasource/DataSource.php +++ b/lib/Cake/Model/Datasource/DataSource.php @@ -185,12 +185,12 @@ class DataSource extends Object { * * To-be-overridden in subclasses. * - * @param Model $model The Model to be created. + * @param Model $Model The Model to be created. * @param array $fields An Array of fields to be saved. * @param array $values An Array of values to save. * @return boolean success */ - public function create(Model $model, $fields = null, $values = null) { + public function create(Model $Model, $fields = null, $values = null) { return false; } @@ -199,12 +199,12 @@ class DataSource extends Object { * * To-be-overridden in subclasses. * - * @param Model $model The model being read. + * @param Model $Model The model being read. * @param array $queryData An array of query data used to find the data you want * @param integer $recursive Number of levels of association * @return mixed */ - public function read(Model $model, $queryData = array(), $recursive = null) { + public function read(Model $Model, $queryData = array(), $recursive = null) { return false; } @@ -213,13 +213,13 @@ class DataSource extends Object { * * To-be-overridden in subclasses. * - * @param Model $model Instance of the model class being updated + * @param Model $Model Instance of the model class being updated * @param array $fields Array of fields to be updated * @param array $values Array of values to be update $fields to. * @param mixed $conditions * @return boolean Success */ - public function update(Model $model, $fields = null, $values = null, $conditions = null) { + public function update(Model $Model, $fields = null, $values = null, $conditions = null) { return false; } @@ -228,11 +228,11 @@ class DataSource extends Object { * * To-be-overridden in subclasses. * - * @param Model $model The model class having record(s) deleted + * @param Model $Model The model class having record(s) deleted * @param mixed $conditions The conditions to use for deleting. * @return boolean Success */ - public function delete(Model $model, $conditions = null) { + public function delete(Model $Model, $conditions = null) { return false; } @@ -321,94 +321,89 @@ class DataSource extends Object { * @param string $query Query string needing replacements done. * @param array $data Array of data with values that will be inserted in placeholders. * @param string $association Name of association model being replaced - * @param array $assocData - * @param Model $model Instance of the model to replace $__cakeID__$ - * @param Model $linkModel Instance of model to replace $__cakeForeignKey__$ + * @param Model $Model Model instance * @param array $stack * @return string String of query data with placeholders replaced. */ - public function insertQueryData($query, $data, $association, $assocData, Model $model, Model $linkModel, $stack) { + public function insertQueryData($query, $data, $association, Model $Model, $stack) { $keys = array('{$__cakeID__$}', '{$__cakeForeignKey__$}'); + $modelAlias = $Model->alias; + foreach ($keys as $key) { - $val = null; - $type = null; - - if (strpos($query, $key) !== false) { - switch ($key) { - case '{$__cakeID__$}': - if (isset($data[$model->alias]) || isset($data[$association])) { - if (isset($data[$model->alias][$model->primaryKey])) { - $val = $data[$model->alias][$model->primaryKey]; - } elseif (isset($data[$association][$model->primaryKey])) { - $val = $data[$association][$model->primaryKey]; - } - } else { - $found = false; - foreach (array_reverse($stack) as $assoc) { - if (isset($data[$assoc]) && isset($data[$assoc][$model->primaryKey])) { - $val = $data[$assoc][$model->primaryKey]; - $found = true; - break; - } - } - if (!$found) { - $val = ''; - } - } - $type = $model->getColumnType($model->primaryKey); - break; - case '{$__cakeForeignKey__$}': - foreach ($model->associations() as $name) { - foreach ($model->$name as $assocName => $assoc) { - if ($assocName === $association) { - if (isset($assoc['foreignKey'])) { - $foreignKey = $assoc['foreignKey']; - $assocModel = $model->$assocName; - $type = $assocModel->getColumnType($assocModel->primaryKey); - - if (isset($data[$model->alias][$foreignKey])) { - $val = $data[$model->alias][$foreignKey]; - } elseif (isset($data[$association][$foreignKey])) { - $val = $data[$association][$foreignKey]; - } else { - $found = false; - foreach (array_reverse($stack) as $assoc) { - if (isset($data[$assoc]) && isset($data[$assoc][$foreignKey])) { - $val = $data[$assoc][$foreignKey]; - $found = true; - break; - } - } - if (!$found) { - $val = ''; - } - } - } - break 3; - } - } - } - break; - } - if (empty($val) && $val !== '0') { - return false; - } - $query = str_replace($key, $this->value($val, $type), $query); + if (strpos($query, $key) === false) { + continue; } + + $insertKey = $InsertModel = null; + switch ($key) { + case '{$__cakeID__$}': + $InsertModel = $Model; + $insertKey = $Model->primaryKey; + + break; + case '{$__cakeForeignKey__$}': + foreach ($Model->associations() as $type) { + foreach ($Model->{$type} as $assoc => $assocData) { + if ($assoc !== $association) { + continue; + } + + if (isset($assocData['foreignKey'])) { + $InsertModel = $Model->{$assoc}; + $insertKey = $assocData['foreignKey']; + } + + break 3; + } + } + + break; + } + + $val = $dataType = null; + if (!empty($insertKey) && !empty($InsertModel)) { + if (isset($data[$modelAlias][$insertKey])) { + $val = $data[$modelAlias][$insertKey]; + } elseif (isset($data[$association][$insertKey])) { + $val = $data[$association][$insertKey]; + } else { + $found = false; + foreach (array_reverse($stack) as $assocData) { + if (isset($data[$assocData]) && isset($data[$assocData][$insertKey])) { + $val = $data[$assocData][$insertKey]; + $found = true; + break; + } + } + + if (!$found) { + $val = ''; + } + } + + $dataType = $InsertModel->getColumnType($InsertModel->primaryKey); + } + + if (empty($val) && $val !== '0') { + return false; + } + + $query = str_replace($key, $this->value($val, $dataType), $query); } + return $query; } /** * To-be-overridden in subclasses. * - * @param Model $model Model instance + * @param Model $Model Model instance * @param string $key Key name to make * @return string Key name for model. */ - public function resolveKey(Model $model, $key) { - return $model->alias . $key; + public function resolveKey(Model $Model, $key) { + return $Model->alias . $key; } /** diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index da041694e..b42f4a1e2 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -636,6 +636,7 @@ class DboSource extends DataSource { /** * Returns an array of all result rows for a given SQL query. + * * Returns false if no rows matched. * * ### Options @@ -1225,7 +1226,7 @@ class DboSource extends DataSource { if ($type === 'hasMany' && empty($assocData['limit']) && !empty($assocData['foreignKey'])) { $ins = $fetch = array(); foreach ($resultSet as &$result) { - if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $assocData, $Model, $LinkModel, $stack)) { + if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $Model, $stack)) { $ins[] = $in; } } @@ -1258,7 +1259,7 @@ class DboSource extends DataSource { } elseif ($type === 'hasAndBelongsToMany') { $ins = $fetch = array(); foreach ($resultSet as &$result) { - if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $assocData, $Model, $LinkModel, $stack)) { + if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $Model, $stack)) { $ins[] = $in; } } @@ -1278,7 +1279,7 @@ class DboSource extends DataSource { $joinKeys = array($foreignKey, $Model->hasAndBelongsToMany[$association]['associationForeignKey']); list($with, $habtmFields) = $Model->joinModel($Model->hasAndBelongsToMany[$association]['with'], $joinKeys); $habtmFieldsCount = count($habtmFields); - $q = $this->insertQueryData($query, null, $association, $assocData, $Model, $LinkModel, $stack); + $q = $this->insertQueryData($query, null, $association, $Model, $stack); if ($q !== false) { $fetch = $this->fetchAll($q, $Model->cacheQueries); @@ -1296,7 +1297,8 @@ class DboSource extends DataSource { foreach ($resultSet as &$row) { if ($type !== 'hasAndBelongsToMany') { - $q = $this->insertQueryData($query, $row, $association, $assocData, $Model, $LinkModel, $stack); + $q = $this->insertQueryData($query, $row, $association, $Model, $stack); + $fetch = null; if ($q !== false) {