Skip to content
Browse files

Gii - check tableNames for reserved PHP keywords when '*' is used

  • Loading branch information...
1 parent 62ccd12 commit 4273483ecd0cb462d4f06976d020609a5452689e mdomba committed
Showing with 86 additions and 79 deletions.
  1. +1 −0 CHANGELOG
  2. +79 −78 framework/gii/CCodeModel.php
  3. +6 −1 framework/gii/generators/model/ModelCode.php
View
1 CHANGELOG
@@ -7,6 +7,7 @@ Version 1.1.10 work in progress
- Bug: Fixed the bug introduced in 1.1.9 CActiveForm required field was not validated if left empty (mdomba)
- Bug: #3070: Fixed the CACtiveForm JS error if there is no field rendered (mdomba)
- Bug: #3071: Fixed the bug that afterValidateAttribute was not called properly (mdomba)
+- Enh: #3063: Gii, when generating models, tableNames will be checked against reserved PHP keywords when '*' is used (mdomba)
Version 1.1.9 January 1, 2012
-----------------------------
View
157 framework/gii/CCodeModel.php
@@ -42,6 +42,84 @@
const STATUS_SUCCESS=3;
const STATUS_ERROR=4;
+ static $keywords=array(
+ '__class__',
+ '__dir__',
+ '__file__',
+ '__function__',
+ '__line__',
+ '__method__',
+ '__namespace__',
+ 'abstract',
+ 'and',
+ 'array',
+ 'as',
+ 'break',
+ 'case',
+ 'catch',
+ 'cfunction',
+ 'class',
+ 'clone',
+ 'const',
+ 'continue',
+ 'declare',
+ 'default',
+ 'die',
+ 'do',
+ 'echo',
+ 'else',
+ 'elseif',
+ 'empty',
+ 'enddeclare',
+ 'endfor',
+ 'endforeach',
+ 'endif',
+ 'endswitch',
+ 'endwhile',
+ 'eval',
+ 'exception',
+ 'exit',
+ 'extends',
+ 'final',
+ 'final',
+ 'for',
+ 'foreach',
+ 'function',
+ 'global',
+ 'goto',
+ 'if',
+ 'implements',
+ 'include',
+ 'include_once',
+ 'instanceof',
+ 'interface',
+ 'isset',
+ 'list',
+ 'namespace',
+ 'new',
+ 'old_function',
+ 'or',
+ 'parent',
+ 'php_user_filter',
+ 'print',
+ 'private',
+ 'protected',
+ 'public',
+ 'require',
+ 'require_once',
+ 'return',
+ 'static',
+ 'switch',
+ 'this',
+ 'throw',
+ 'try',
+ 'unset',
+ 'use',
+ 'var',
+ 'while',
+ 'xor',
+ );
+
/**
* @var array user confirmations on whether to overwrite existing code files with the newly generated ones.
* The value of this property is internally managed by this class and {@link CCodeGenerator}.
@@ -386,85 +464,8 @@ public function class2var($name)
*/
public function validateReservedWord($attribute,$params)
{
- static $keywords=array(
- '__class__',
- '__dir__',
- '__file__',
- '__function__',
- '__line__',
- '__method__',
- '__namespace__',
- 'abstract',
- 'and',
- 'array',
- 'as',
- 'break',
- 'case',
- 'catch',
- 'cfunction',
- 'class',
- 'clone',
- 'const',
- 'continue',
- 'declare',
- 'default',
- 'die',
- 'do',
- 'echo',
- 'else',
- 'elseif',
- 'empty',
- 'enddeclare',
- 'endfor',
- 'endforeach',
- 'endif',
- 'endswitch',
- 'endwhile',
- 'eval',
- 'exception',
- 'exit',
- 'extends',
- 'final',
- 'final',
- 'for',
- 'foreach',
- 'function',
- 'global',
- 'goto',
- 'if',
- 'implements',
- 'include',
- 'include_once',
- 'instanceof',
- 'interface',
- 'isset',
- 'list',
- 'namespace',
- 'new',
- 'old_function',
- 'or',
- 'parent',
- 'php_user_filter',
- 'print',
- 'private',
- 'protected',
- 'public',
- 'require',
- 'require_once',
- 'return',
- 'static',
- 'switch',
- 'this',
- 'throw',
- 'try',
- 'unset',
- 'use',
- 'var',
- 'while',
- 'xor',
- );
$value=$this->$attribute;
- if(in_array(strtolower($value),$keywords))
+ if(in_array(strtolower($value),self::$keywords))
$this->addError($attribute, $this->getAttributeLabel($attribute).' cannot take a reserved PHP keyword.');
}
}
View
7 framework/gii/generators/model/ModelCode.php
@@ -109,6 +109,7 @@ public function prepare()
public function validateTableName($attribute,$params)
{
+ $invalidTables=array();
$invalidColumns=array();
if($this->tableName[strlen($this->tableName)-1]==='*')
@@ -124,6 +125,8 @@ public function validateTableName($attribute,$params)
{
if($this->tablePrefix=='' || strpos($table->name,$this->tablePrefix)===0)
{
+ if(in_array(strtolower($table->name),self::$keywords))
+ $invalidTables[]=$table->name;
if(($invalidColumn=$this->checkColumns($table))!==null)
$invalidColumns[]=$invalidColumn;
}
@@ -140,8 +143,10 @@ public function validateTableName($attribute,$params)
$invalidColumns[]=$invalidColumn;
}
+ if($invalidTables!=array())
+ $this->addError('tableName', 'Model class cannot take a reserved PHP keyword! Table name: '.implode(', ', $invalidTables).".");
if($invalidColumns!=array())
- $this->addError('tableName',"Column names that does not follow PHP variable naming convention: ".implode(', ', $invalidColumns)."." );
+ $this->addError('tableName', 'Column names that does not follow PHP variable naming convention: '.implode(', ', $invalidColumns).".");
}
/*

0 comments on commit 4273483

Please sign in to comment.
Something went wrong with that request. Please try again.