Skip to content

Commit

Permalink
Use constants instead of bare strings.
Browse files Browse the repository at this point in the history
Constants feel better in code vs. bare strings. They also
cause errors when you make a mistake.
  • Loading branch information
markstory committed Jun 7, 2013
1 parent c0f7ef8 commit 5d29549
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
12 changes: 6 additions & 6 deletions lib/Cake/Database/Schema/SqliteSchema.php
Expand Up @@ -229,9 +229,9 @@ protected function _convertOnClause($clause) {
return strtolower($clause); return strtolower($clause);
} }
if ($clause === 'NO ACTION') { if ($clause === 'NO ACTION') {
return 'noAction'; return Table::ACTION_NO_ACTION;
} }
return 'setNull'; return Table::ACTION_SET_NULL;
} }


/** /**
Expand Down Expand Up @@ -347,16 +347,16 @@ public function constraintSql(Table $table, $name) {
* @return string * @return string
*/ */
protected function _foreignOnClause($on) { protected function _foreignOnClause($on) {
if ($on === 'setNull') { if ($on === Table::ACTION_SET_NULL) {
return 'SET NULL'; return 'SET NULL';
} }
if ($on === 'cascade') { if ($on === Table::ACTION_CASCADE) {
return 'CASCADE'; return 'CASCADE';
} }
if ($on === 'restrict') { if ($on === Table::ACTION_RESTRICT) {
return 'RESTRICT'; return 'RESTRICT';
} }
if ($on === 'noAction') { if ($on === Table::ACTION_NO_ACTION) {
return 'NO ACTION'; return 'NO ACTION';
} }
} }
Expand Down
11 changes: 8 additions & 3 deletions lib/Cake/Database/Schema/Table.php
Expand Up @@ -125,6 +125,11 @@ class Table {
const INDEX_INDEX = 'index'; const INDEX_INDEX = 'index';
const INDEX_FULLTEXT = 'fulltext'; const INDEX_FULLTEXT = 'fulltext';


const ACTION_CASCADE = 'cascade';
const ACTION_SET_NULL = 'setNull';
const ACTION_NO_ACTION = 'noAction';
const ACTION_RESTRICT = 'restrict';

/** /**
* Constructor. * Constructor.
* *
Expand Down Expand Up @@ -288,8 +293,8 @@ public function primaryKey() {
* - `type` The type of constraint being added. * - `type` The type of constraint being added.
* - `columns` The columns in the index. * - `columns` The columns in the index.
* - `references` The table, column a foreign key references. * - `references` The table, column a foreign key references.
* - `update` The behavior on update. Options are 'restrict', `null`, 'cascade', 'none'. * - `update` The behavior on update. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
* - `delete` The behavior on delete. Options are 'restrict', `null`, 'cascade', 'none'. * - `delete` The behavior on delete. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
* *
* The default for 'update' & 'delete' is 'cascade'. * The default for 'update' & 'delete' is 'cascade'.
* *
Expand Down Expand Up @@ -331,7 +336,7 @@ protected function _checkForeignKey($attrs) {
if (count($attrs['references']) < 2) { if (count($attrs['references']) < 2) {
throw new Exception(__d('cake_dev', 'References must contain a table and column.')); throw new Exception(__d('cake_dev', 'References must contain a table and column.'));
} }
$validActions = ['cascade', 'restrict', 'setNull', 'noAction']; $validActions = [static::ACTION_CASCADE, static::ACTION_RESTRICT, static::ACTION_SET_NULL, static::ACTION_NO_ACTION];
if (!in_array($attrs['update'], $validActions)) { if (!in_array($attrs['update'], $validActions)) {
throw new Exception(__d('cake_dev', 'Update action is invalid. Must be one of %s', implode(',', $validActions))); throw new Exception(__d('cake_dev', 'Update action is invalid. Must be one of %s', implode(',', $validActions)));
} }
Expand Down

0 comments on commit 5d29549

Please sign in to comment.