mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Keep insertQueryData() DRY, issue less lookups. Remove unused $assocData
and $LinkModel from the signature. Model objects should be CamelCase.
This commit is contained in:
parent
52d425737a
commit
0e74a0757a
2 changed files with 80 additions and 83 deletions
|
@ -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__$}');
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$val = null;
|
||||
$type = null;
|
||||
$modelAlias = $Model->alias;
|
||||
|
||||
if (strpos($query, $key) !== false) {
|
||||
foreach ($keys as $key) {
|
||||
if (strpos($query, $key) === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$insertKey = $InsertModel = null;
|
||||
switch ($key) {
|
||||
case '{$__cakeID__$}':
|
||||
if (isset($data[$model->alias]) || isset($data[$association])) {
|
||||
if (isset($data[$model->alias][$model->primaryKey])) {
|
||||
$val = $data[$model->alias][$model->primaryKey];
|
||||
} elseif (isset($data[$association][$model->primaryKey])) {
|
||||
$val = $data[$association][$model->primaryKey];
|
||||
}
|
||||
} else {
|
||||
$found = false;
|
||||
foreach (array_reverse($stack) as $assoc) {
|
||||
if (isset($data[$assoc]) && isset($data[$assoc][$model->primaryKey])) {
|
||||
$val = $data[$assoc][$model->primaryKey];
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
$val = '';
|
||||
}
|
||||
}
|
||||
$type = $model->getColumnType($model->primaryKey);
|
||||
$InsertModel = $Model;
|
||||
$insertKey = $Model->primaryKey;
|
||||
|
||||
break;
|
||||
case '{$__cakeForeignKey__$}':
|
||||
foreach ($model->associations() as $name) {
|
||||
foreach ($model->$name as $assocName => $assoc) {
|
||||
if ($assocName === $association) {
|
||||
if (isset($assoc['foreignKey'])) {
|
||||
$foreignKey = $assoc['foreignKey'];
|
||||
$assocModel = $model->$assocName;
|
||||
$type = $assocModel->getColumnType($assocModel->primaryKey);
|
||||
foreach ($Model->associations() as $type) {
|
||||
foreach ($Model->{$type} as $assoc => $assocData) {
|
||||
if ($assoc !== $association) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($data[$model->alias][$foreignKey])) {
|
||||
$val = $data[$model->alias][$foreignKey];
|
||||
} elseif (isset($data[$association][$foreignKey])) {
|
||||
$val = $data[$association][$foreignKey];
|
||||
} else {
|
||||
$found = false;
|
||||
foreach (array_reverse($stack) as $assoc) {
|
||||
if (isset($data[$assoc]) && isset($data[$assoc][$foreignKey])) {
|
||||
$val = $data[$assoc][$foreignKey];
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
$val = '';
|
||||
}
|
||||
}
|
||||
if (isset($assocData['foreignKey'])) {
|
||||
$InsertModel = $Model->{$assoc};
|
||||
$insertKey = $assocData['foreignKey'];
|
||||
}
|
||||
|
||||
break 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$val = $dataType = null;
|
||||
if (!empty($insertKey) && !empty($InsertModel)) {
|
||||
if (isset($data[$modelAlias][$insertKey])) {
|
||||
$val = $data[$modelAlias][$insertKey];
|
||||
} elseif (isset($data[$association][$insertKey])) {
|
||||
$val = $data[$association][$insertKey];
|
||||
} else {
|
||||
$found = false;
|
||||
foreach (array_reverse($stack) as $assocData) {
|
||||
if (isset($data[$assocData]) && isset($data[$assocData][$insertKey])) {
|
||||
$val = $data[$assocData][$insertKey];
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$found) {
|
||||
$val = '';
|
||||
}
|
||||
}
|
||||
|
||||
$dataType = $InsertModel->getColumnType($InsertModel->primaryKey);
|
||||
}
|
||||
|
||||
if (empty($val) && $val !== '0') {
|
||||
return false;
|
||||
}
|
||||
$query = str_replace($key, $this->value($val, $type), $query);
|
||||
}
|
||||
|
||||
$query = str_replace($key, $this->value($val, $dataType), $query);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* To-be-overridden in subclasses.
|
||||
*
|
||||
* @param Model $model Model instance
|
||||
* @param Model $Model Model instance
|
||||
* @param string $key Key name to make
|
||||
* @return string Key name for model.
|
||||
*/
|
||||
public function resolveKey(Model $model, $key) {
|
||||
return $model->alias . $key;
|
||||
public function resolveKey(Model $Model, $key) {
|
||||
return $Model->alias . $key;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue