From 3c88d817352d2217c6725cf09f48fa5eefe89379 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 1 Jan 2010 23:34:11 -0500 Subject: [PATCH 1/4] Adding tests for model method validation with multiple parameters and addition of original validation rule to method params. Closes #149 --- .../libs/model/model_validation.test.php | 50 +++++++++++++++++++ cake/tests/cases/libs/model/models.php | 10 ++++ 2 files changed, 60 insertions(+) diff --git a/cake/tests/cases/libs/model/model_validation.test.php b/cake/tests/cases/libs/model/model_validation.test.php index a35e4898d..54aa474e3 100644 --- a/cake/tests/cases/libs/model/model_validation.test.php +++ b/cake/tests/cases/libs/model/model_validation.test.php @@ -73,6 +73,56 @@ class ModelValidationTest extends BaseModelTest { ); $this->assertEqual($TestModel->invalidFields(), $expected); + + $TestModel->validate['title'] = array( + 'rule' => array('customValidatorWithSixParams', 'one', 'two', null, 'four'), + 'required' => true + ); + $TestModel->create(array('title' => 'foo')); + $TestModel->invalidFields(); + $expected = array( + 'data' => array( + 'title' => 'foo' + ), + 'one' => 'one', + 'two' => 'two', + 'three' => null, + 'four' => 'four', + 'five' => array( + 'rule' => array(1 => 'one', 2 => 'two', 3 => null, 4 => 'four'), + 'on' => null, + 'last' => false, + 'allowEmpty' => false, + 'required' => true + ), + 'six' => 6 + ); + $this->assertEqual($TestModel->validatorParams, $expected); + + $TestModel->validate['title'] = array( + 'rule' => array('customValidatorWithSixParams', 'one', array('two'), null, 'four', array('five' => 5)), + 'required' => true + ); + $TestModel->create(array('title' => 'foo')); + $TestModel->invalidFields(); + $expected = array( + 'data' => array( + 'title' => 'foo' + ), + 'one' => 'one', + 'two' => array('two'), + 'three' => null, + 'four' => 'four', + 'five' => array('five' => 5), + 'six' => array( + 'rule' => array(1 => 'one', 2 => array('two'), 3 => null, 4 => 'four', 5 => array('five' => 5)), + 'on' => null, + 'last' => false, + 'allowEmpty' => false, + 'required' => true + ) + ); + $this->assertEqual($TestModel->validatorParams, $expected); } /** * Tests validation parameter fieldList in invalidFields diff --git a/cake/tests/cases/libs/model/models.php b/cake/tests/cases/libs/model/models.php index 33ca715fa..cb2089679 100644 --- a/cake/tests/cases/libs/model/models.php +++ b/cake/tests/cases/libs/model/models.php @@ -1939,6 +1939,16 @@ class ValidationTest1 extends CakeTestModel { function customValidatorWithMessage($data) { return 'This field will *never* validate! Muhahaha!'; } +/** + * Test validation with many parameters + * + * @return void + */ + function customValidatorWithSixParams($data, $one = 1, $two = 2, $three = 3, $four = 4, $five = 5, $six = 6) { + $this->validatorParams = get_defined_vars(); + unset($this->validatorParams['this']); + return true; + } } /** * ValidationTest2 class From 026eeb645b6959115bacd0bf690db9edfb5ac167 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 1 Jan 2010 23:58:07 -0500 Subject: [PATCH 2/4] Correcting and improving doc block for Model::__construct. --- cake/libs/model/model.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index f0e0a6e9c..c7fd52c6c 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -333,9 +333,34 @@ class Model extends Overloadable { /** * Constructor. Binds the model's database table to the object. * - * @param integer $id Set this ID for this model on startup + * If `$id` is an array it can be used to pass several options into the model. + * + * - id - The id to start the model on. + * - table - The table to use for this model. + * - ds - The connection name this model is connected to. + * - name - The name of the model eg. Post. + * - alias - The alias of the model, this is used for registering the instance in the `ClassRegistry`. + * eg. `ParentThread` + * + * ### Overriding Model's __construct method. + * + * When overriding Model::__construct() be careful to include and pass in all 3 of the + * arguments to `parent::__construct($id, $table, $ds);` + * + * ### Dynamically creating models + * + * You can dynamically create model instances using the the $id array syntax. + * + * {{{ + * $Post = new Model(array('table' => 'posts', 'name' => 'Post', 'ds' => 'connection2')); + * }}} + * + * Would create a model attached to the posts table on connection2. Dynamic model creation is useful + * when you want a model object that contains no associations or attached behaviors. + * + * @param mixed $id Set this ID for this model on startup, can also be an array of options, see above. * @param string $table Name of database table to use. - * @param object $ds DataSource connection object. + * @param string $ds DataSource connection name. */ function __construct($id = false, $table = null, $ds = null) { parent::__construct(); From 730153b1516f368a73dcae8efcfa025b9187b062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Perras?= Date: Thu, 7 Jan 2010 17:49:57 -0500 Subject: [PATCH 3/4] Adding isset() checks for CakeSchema::compare() when diff'ing db indexes. Allows for slightly better interoperability between schemas generated with CakePHP 1.2 and with 1.3 --- cake/libs/model/schema.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cake/libs/model/schema.php b/cake/libs/model/schema.php index d607dbf57..d1a455290 100644 --- a/cake/libs/model/schema.php +++ b/cake/libs/model/schema.php @@ -168,7 +168,7 @@ class CakeSchema extends Object { * Reads database and creates schema tables * * Options - * + * * - 'connection' - the db connection to use * - 'name' - name of the schema * - 'models' - a list of models to use, or false to ignore models @@ -428,8 +428,12 @@ class CakeSchema extends Object { if (isset($old[$table]['indexes']) && isset($new[$table]['indexes'])) { $diff = $this->_compareIndexes($new[$table]['indexes'], $old[$table]['indexes']); if ($diff) { - $tables[$table]['drop']['indexes'] = $diff['drop']; - $tables[$table]['add']['indexes'] = $diff['add']; + if (isset($tables[$table]['drop']['indexes']) && isset($diff['drop'])) { + $tables[$table]['drop']['indexes'] = $diff['drop']; + } + if (isset($tables[$table]['add']['indexes']) && isset($diff['add'])) { + $tables[$table]['add']['indexes'] = $diff['add']; + } } } } From 11a5403aaf4049b4a3e605b14057ba17ff093990 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 8 Jan 2010 18:35:01 -0500 Subject: [PATCH 4/4] Removing unused variables from inflections.php. These variables are artifacts from previous refactorings and have been non-functional for quite sometime. Fixes #163 --- app/config/inflections.php | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/app/config/inflections.php b/app/config/inflections.php index ed8c08b3d..07ca6d2a8 100644 --- a/app/config/inflections.php +++ b/app/config/inflections.php @@ -53,18 +53,5 @@ * $singularRules = array('/(s)tatuses$/i' => '\1\2tatus', '/(matr)ices$/i' =>'\1ix','/(vert|ind)ices$/i') */ $singularRules = array(); -/** - * This is a key only array of singular words that should not be inflected. - * You should not have to change this value below if you do change it use same format - * as the $uninflectedPlural above. - */ - $uninflectedSingular = $uninflectedPlural; -/** - * This is a key => value array of singular irregular words. - * Most of the time this will be a reverse of the above $irregularPlural array - * You should not have to change this value below if you do change it use same format - * - * $irregularSingular = array('atlases' => 'atlas', 'beefs' => 'beef', 'brothers' => 'brother') - */ - $irregularSingular = array_flip($irregularPlural); + ?> \ No newline at end of file