Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixing most coding standard issues in Model/
  • Loading branch information
markstory committed Mar 5, 2012
1 parent 9b395eb commit 9a6ad7e
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 85 deletions.
2 changes: 2 additions & 0 deletions lib/Cake/Model/AclNode.php
Expand Up @@ -56,6 +56,7 @@ public function __construct() {
*
* @param mixed $ref Array with 'model' and 'foreign_key', model object, or string value
* @return array Node found in database
* @throws CakeException when binding to a model that doesn't exist.
*/
public function node($ref = null) {
$db = $this->getDataSource();
Expand Down Expand Up @@ -177,4 +178,5 @@ public function node($ref = null) {
}
return $result;
}

}
1 change: 1 addition & 0 deletions lib/Cake/Model/Behavior/AclBehavior.php
Expand Up @@ -137,4 +137,5 @@ public function afterDelete(Model $model) {
}
}
}

}
7 changes: 4 additions & 3 deletions lib/Cake/Model/Behavior/ContainableBehavior.php
Expand Up @@ -186,7 +186,7 @@ public function beforeFind(Model $Model, $query) {
if (!empty($Model->{$type})) {
foreach ($Model->{$type} as $assoc => $data) {
if ($Model->useDbConfig == $Model->{$assoc}->useDbConfig && !empty($data['fields'])) {
foreach ((array) $data['fields'] as $field) {
foreach ((array)$data['fields'] as $field) {
$query['fields'][] = (strpos($field, '.') === false ? $assoc . '.' : '') . $field;
}
}
Expand Down Expand Up @@ -286,7 +286,7 @@ public function containments(Model $Model, $contain, $containments = array(), $t
$children = (array)$children;
foreach ($children as $key => $val) {
if (is_string($key) && is_string($val) && !in_array($key, $options, true)) {
$children[$key] = (array) $val;
$children[$key] = (array)$val;
}
}

Expand Down Expand Up @@ -326,7 +326,7 @@ public function containments(Model $Model, $contain, $containments = array(), $t
}
if ($optionKey && isset($children[$key])) {
if (!empty($keep[$name][$key]) && is_array($keep[$name][$key])) {
$keep[$name][$key] = array_merge((isset($keep[$name][$key]) ? $keep[$name][$key] : array()), (array) $children[$key]);
$keep[$name][$key] = array_merge((isset($keep[$name][$key]) ? $keep[$name][$key] : array()), (array)$children[$key]);
} else {
$keep[$name][$key] = $children[$key];
}
Expand Down Expand Up @@ -424,4 +424,5 @@ public function containmentsMap($containments) {
}
return $map;
}

}
33 changes: 18 additions & 15 deletions lib/Cake/Model/Behavior/TranslateBehavior.php
Expand Up @@ -120,15 +120,15 @@ public function beforeFind(Model $model, $query) {
$this->_runtimeModel = $RuntimeModel;

if (is_string($query['fields']) && 'COUNT(*) AS ' . $db->name('count') == $query['fields']) {
$query['fields'] = 'COUNT(DISTINCT('.$db->name($model->alias . '.' . $model->primaryKey) . ')) ' . $db->alias . 'count';
$query['fields'] = 'COUNT(DISTINCT(' . $db->name($model->alias . '.' . $model->primaryKey) . ')) ' . $db->alias . 'count';
$query['joins'][] = array(
'type' => 'INNER',
'alias' => $RuntimeModel->alias,
'table' => $joinTable,
'conditions' => array(
$model->alias . '.' . $model->primaryKey => $db->identifier($RuntimeModel->alias.'.foreign_key'),
$RuntimeModel->alias.'.model' => $model->name,
$RuntimeModel->alias.'.locale' => $locale
$model->alias . '.' . $model->primaryKey => $db->identifier($RuntimeModel->alias . '.foreign_key'),
$RuntimeModel->alias . '.model' => $model->name,
$RuntimeModel->alias . '.locale' => $locale
)
);
$conditionFields = $this->_checkConditions($model, $query);
Expand All @@ -147,7 +147,7 @@ public function beforeFind(Model $model, $query) {
foreach ($fields as $key => $value) {
$field = (is_numeric($key)) ? $value : $key;

if (in_array($model->alias.'.*', $query['fields']) || in_array($model->alias.'.' . $field, $query['fields']) || in_array($field, $query['fields'])) {
if (in_array($model->alias . '.*', $query['fields']) || in_array($model->alias . '.' . $field, $query['fields']) || in_array($field, $query['fields'])) {
$addFields[] = $field;
}
}
Expand Down Expand Up @@ -221,34 +221,34 @@ protected function _addJoin(Model $model, $query, $field, $aliasField, $locale,
foreach ($locale as $_locale) {
$model->virtualFields['i18n_' . $field . '_' . $_locale] = 'I18n__' . $field . '__' . $_locale . '.content';
if (!empty($query['fields']) && is_array($query['fields'])) {
$query['fields'][] = 'i18n_'.$field.'_'.$_locale;
$query['fields'][] = 'i18n_' . $field . '_' . $_locale;
}
$query['joins'][] = array(
'type' => 'LEFT',
'alias' => 'I18n__'.$field.'__'.$_locale,
'alias' => 'I18n__' . $field . '__' . $_locale,
'table' => $joinTable,
'conditions' => array(
$model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}__{$_locale}.foreign_key"),
'I18n__'.$field.'__'.$_locale.'.model' => $model->name,
'I18n__'.$field.'__'.$_locale.'.'.$RuntimeModel->displayField => $aliasField,
'I18n__'.$field.'__'.$_locale.'.locale' => $_locale
'I18n__' . $field . '__' . $_locale . '.model' => $model->name,
'I18n__' . $field . '__' . $_locale . '.' . $RuntimeModel->displayField => $aliasField,
'I18n__' . $field . '__' . $_locale . '.locale' => $_locale
)
);
}
} else {
$model->virtualFields['i18n_' . $field] = 'I18n__' . $field . '.content';
if (!empty($query['fields']) && is_array($query['fields'])) {
$query['fields'][] = 'i18n_'.$field;
$query['fields'][] = 'i18n_' . $field;
}
$query['joins'][] = array(
'type' => 'INNER',
'alias' => 'I18n__'.$field,
'alias' => 'I18n__' . $field,
'table' => $joinTable,
'conditions' => array(
$model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}.foreign_key"),
'I18n__'.$field.'.model' => $model->name,
'I18n__'.$field.'.'.$RuntimeModel->displayField => $aliasField,
'I18n__'.$field.'.locale' => $locale
'I18n__' . $field . '.model' => $model->name,
'I18n__' . $field . '.' . $RuntimeModel->displayField => $aliasField,
'I18n__' . $field . '.locale' => $locale
)
);
}
Expand Down Expand Up @@ -446,6 +446,8 @@ public function translateModel(Model $model) {
* @param string|array $fields string with field or array(field1, field2=>AssocName, field3)
* @param boolean $reset
* @return boolean
* @throws CakeException when attempting to bind a translating called name. This is not allowed
* as it shadows Model::$name.
*/
public function bindTranslation(Model $model, $fields, $reset = true) {
if (is_string($fields)) {
Expand Down Expand Up @@ -570,5 +572,6 @@ public function unbindTranslation(Model $model, $fields = null) {
}
return true;
}

}

17 changes: 9 additions & 8 deletions lib/Cake/Model/Behavior/TreeBehavior.php
Expand Up @@ -86,7 +86,7 @@ public function afterSave(Model $Model, $created) {
if ((isset($Model->data[$Model->alias][$parent])) && $Model->data[$Model->alias][$parent]) {
return $this->_setParent($Model, $Model->data[$Model->alias][$parent], $created);
}
} elseif ($__parentChange) {
} elseif ($this->settings[$Model->alias]['__parentChange']) {
$this->settings[$Model->alias]['__parentChange'] = false;
return $this->_setParent($Model, $Model->data[$Model->alias][$parent]);
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public function beforeFind(Model $Model, $query) {
public function beforeDelete(Model $Model, $cascade = true) {
extract($this->settings[$Model->alias]);
$data = current($Model->find('first', array(
'conditions' => array($Model->alias . '.' . $Model->primaryKey => $Model->id),
'conditions' => array($Model->alias . '.' . $Model->primaryKey => $Model->id),
'fields' => array($Model->alias . '.' . $left, $Model->alias . '.' . $right),
'recursive' => -1)));

Expand Down Expand Up @@ -163,8 +163,8 @@ public function beforeSave(Model $Model) {
return false;
}
list($parentNode) = array_values($parentNode);
$Model->data[$Model->alias][$left] = 0; //$parentNode[$right];
$Model->data[$Model->alias][$right] = 0; //$parentNode[$right] + 1;
$Model->data[$Model->alias][$left] = 0;
$Model->data[$Model->alias][$right] = 0;
} else {
$edge = $this->_getMax($Model, $scope, $right, $recursive);
$Model->data[$Model->alias][$left] = $edge + 1;
Expand Down Expand Up @@ -357,7 +357,7 @@ public function generateTreeList(Model $Model, $conditions = null, $keyPath = nu
while ($stack && ($stack[count($stack) - 1] < $result[$Model->alias][$right])) {
array_pop($stack);
}
$results[$i]['tree_prefix'] = str_repeat($spacer,count($stack));
$results[$i]['tree_prefix'] = str_repeat($spacer, count($stack));
$stack[] = $result[$Model->alias][$right];
}
if (empty($results)) {
Expand Down Expand Up @@ -542,7 +542,7 @@ public function moveUp(Model $Model, $id = null, $number = 1) {
return false;
}
$edge = $this->_getMax($Model, $scope, $right, $recursive);
$this->_sync($Model, $edge - $previousNode[$left] +1, '+', 'BETWEEN ' . $previousNode[$left] . ' AND ' . $previousNode[$right]);
$this->_sync($Model, $edge - $previousNode[$left] + 1, '+', 'BETWEEN ' . $previousNode[$left] . ' AND ' . $previousNode[$right]);
$this->_sync($Model, $node[$left] - $previousNode[$left], '-', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]);
$this->_sync($Model, $edge - $previousNode[$left] - ($node[$right] - $node[$left]), '-', '> ' . $edge);
if (is_int($number)) {
Expand Down Expand Up @@ -766,7 +766,7 @@ public function verify(Model $Model) {
}
$min = $this->_getMin($Model, $scope, $left, $recursive);
$edge = $this->_getMax($Model, $scope, $right, $recursive);
$errors = array();
$errors = array();

for ($i = $min; $i <= $edge; $i++) {
$count = $Model->find('count', array('conditions' => array(
Expand Down Expand Up @@ -873,7 +873,7 @@ protected function _setParent(Model $Model, $parentId = null, $created = false)
);
$Model->data = $result;
} else {
$this->_sync($Model, $edge - $node[$left] +1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
$this->_sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
$diff = $node[$right] - $node[$left] + 1;

if ($node[$left] > $parentNode[$left]) {
Expand Down Expand Up @@ -976,4 +976,5 @@ protected function _sync(Model $Model, $shift, $dir = '+', $conditions = array()
$Model->updateAll(array($Model->alias . '.' . $field => $Model->escapeField($field) . ' ' . $dir . ' ' . $shift), $conditions);
$Model->recursive = $ModelRecursive;
}

}
1 change: 1 addition & 0 deletions lib/Cake/Model/BehaviorCollection.php
Expand Up @@ -291,4 +291,5 @@ public function implementedEvents() {
'Model.afterDelete' => 'trigger'
);
}

}
10 changes: 5 additions & 5 deletions lib/Cake/Model/CakeSchema.php
Expand Up @@ -161,21 +161,20 @@ public function load($options = array()) {
$this->build($options);
extract(get_object_vars($this));

$class = $name .'Schema';
$class = $name . 'Schema';

if (!class_exists($class)) {
if (file_exists($path . DS . $file) && is_file($path . DS . $file)) {
require_once($path . DS . $file);
require_once $path . DS . $file;
} elseif (file_exists($path . DS . 'schema.php') && is_file($path . DS . 'schema.php')) {
require_once($path . DS . 'schema.php');
require_once $path . DS . 'schema.php';
}
}

if (class_exists($class)) {
$Schema = new $class($options);
return $Schema;
}

return false;
}

Expand Down Expand Up @@ -207,7 +206,7 @@ public function read($options = array()) {
}

$tables = array();
$currentTables = (array) $db->listSources();
$currentTables = (array)$db->listSources();

$prefix = null;
if (isset($db->config['prefix'])) {
Expand Down Expand Up @@ -703,4 +702,5 @@ protected function _compareIndexes($new, $old) {
protected function _noPrefixTable($prefix, $table) {
return preg_replace('/^' . preg_quote($prefix) . '/', '', $table);
}

}
3 changes: 2 additions & 1 deletion lib/Cake/Model/ConnectionManager.php
Expand Up @@ -189,7 +189,7 @@ public static function enumConnectionObjects() {
if (empty(self::$_init)) {
self::_init();
}
return (array) self::$config;
return (array)self::$config;
}

/**
Expand Down Expand Up @@ -262,4 +262,5 @@ protected static function _connectionData($config) {
}
return compact('package', 'classname', 'plugin');
}

}
1 change: 1 addition & 0 deletions lib/Cake/Model/Datasource/CakeSession.php
Expand Up @@ -682,4 +682,5 @@ protected static function _setError($errorNumber, $errorMessage) {
self::$error[$errorNumber] = $errorMessage;
self::$lastError = $errorNumber;
}

}
1 change: 1 addition & 0 deletions lib/Cake/Model/Datasource/DataSource.php
Expand Up @@ -428,4 +428,5 @@ public function __destruct() {
$this->close();
}
}

}
9 changes: 4 additions & 5 deletions lib/Cake/Model/Datasource/Database/Mysql.php
Expand Up @@ -444,7 +444,7 @@ public function index($model) {
$indices = $this->_execute('SHOW INDEX FROM ' . $table);
while ($idx = $indices->fetch(PDO::FETCH_OBJ)) {
if ($old) {
$idx = (object) current((array)$idx);
$idx = (object)current((array)$idx);
}
if (!isset($index[$idx->Key_name]['column'])) {
$col = array();
Expand Down Expand Up @@ -589,9 +589,9 @@ protected function _alterIndexes($table, $indexes) {
}
}
if (is_array($value['column'])) {
$out .= 'KEY '. $name .' (' . implode(', ', array_map(array(&$this, 'name'), $value['column'])) . ')';
$out .= 'KEY ' . $name . ' (' . implode(', ', array_map(array(&$this, 'name'), $value['column'])) . ')';
} else {
$out .= 'KEY '. $name .' (' . $this->name($value['column']) . ')';
$out .= 'KEY ' . $name . ' (' . $this->name($value['column']) . ')';
}
$alter[] = $out;
}
Expand All @@ -618,7 +618,7 @@ public function listDetailedSources($name = null) {
} else {
$tables = array();
foreach ($result as $row) {
$tables[$row['Name']] = (array) $row;
$tables[$row['Name']] = (array)$row;
unset($tables[$row['Name']]['queryString']);
if (!empty($row['Collation'])) {
$charset = $this->getCharsetName($row['Collation']);
Expand All @@ -635,7 +635,6 @@ public function listDetailedSources($name = null) {
}
}


/**
* Converts database-layer column types to basic types
*
Expand Down
12 changes: 6 additions & 6 deletions lib/Cake/Model/Datasource/Database/Postgres.php
Expand Up @@ -507,16 +507,16 @@ public function alterSchema($compare, $table = null) {
$default = isset($col['default']) ? $col['default'] : null;
$nullable = isset($col['null']) ? $col['null'] : null;
unset($col['default'], $col['null']);
$colList[] = 'ALTER COLUMN '. $fieldName .' TYPE ' . str_replace(array($fieldName, 'NOT NULL'), '', $this->buildColumn($col));
$colList[] = 'ALTER COLUMN ' . $fieldName . ' TYPE ' . str_replace(array($fieldName, 'NOT NULL'), '', $this->buildColumn($col));
if (isset($nullable)) {
$nullable = ($nullable) ? 'DROP NOT NULL' : 'SET NOT NULL';
$colList[] = 'ALTER COLUMN '. $fieldName .' ' . $nullable;
$colList[] = 'ALTER COLUMN ' . $fieldName . ' ' . $nullable;
}

if (isset($default)) {
$colList[] = 'ALTER COLUMN '. $fieldName .' SET DEFAULT ' . $this->value($default, $col['type']);
$colList[] = 'ALTER COLUMN ' . $fieldName . ' SET DEFAULT ' . $this->value($default, $col['type']);
} else {
$colList[] = 'ALTER COLUMN '. $fieldName .' DROP DEFAULT';
$colList[] = 'ALTER COLUMN ' . $fieldName . ' DROP DEFAULT';
}

}
Expand Down Expand Up @@ -760,14 +760,14 @@ public function boolean($data, $quote = false) {
$result = ($data === 'TRUE');
break;
default:
$result = (bool) $data;
$result = (bool)$data;
break;
}

if ($quote) {
return ($result) ? 'TRUE' : 'FALSE';
}
return (bool) $result;
return (bool)$result;
}

/**
Expand Down

0 comments on commit 9a6ad7e

Please sign in to comment.