From 32c1a1f78058c62b70bff2aab1042b2704713001 Mon Sep 17 00:00:00 2001 From: Aaron Peterson Date: Fri, 21 Oct 2011 23:37:33 -0700 Subject: [PATCH 1/2] strict typing based on schema --- Model/Datasource/MongodbSource.php | 16 +++++-- Test/Case/Datasource/MongodbSourceTest.php | 53 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/Model/Datasource/MongodbSource.php b/Model/Datasource/MongodbSource.php index 382bea2..8fa6348 100644 --- a/Model/Datasource/MongodbSource.php +++ b/Model/Datasource/MongodbSource.php @@ -452,6 +452,9 @@ public function create(&$Model, $fields = null, $values = null) { } else { $data = $Model->data; } + foreach ($data as $field => $value) { + $data[$field] = $this->value($value, $Model->getColumnType($field)); + } if (!empty($data['_id'])) { $this->_convertId($data['_id']); } @@ -1159,12 +1162,19 @@ public function mapReduce($query, $timeout = null) { * @return mixed Prepared value or array of values. * @access public */ - public function value($data, $column = null, $read = true) { - $return = parent::value($data, $column, $read); + public function value($data, $column = null) { + /* + $return = parent::value($data, $column); if ($return === null && $data !== null) { return $data; } - return $return; + * + */ + if ($column == 'integer') { + return intval($data); + } + + return $data; } /** diff --git a/Test/Case/Datasource/MongodbSourceTest.php b/Test/Case/Datasource/MongodbSourceTest.php index e0ee453..36004bb 100644 --- a/Test/Case/Datasource/MongodbSourceTest.php +++ b/Test/Case/Datasource/MongodbSourceTest.php @@ -439,6 +439,59 @@ public function testSave() { $this->assertTrue(is_a($resultData['modified'], 'MongoDate')); } + +/** + * Test data types after storage + * http://php.net/manual/en/class.mongodb.php + * + * @return void + * @access public + */ + public function testTypesAfterSave() { + $data = array( + 'title' => 'test', + 'body' => 'aaaa', + 'text' => 'bbbb', + 'count' => 4 // native PHP Mongo casting + ); + $saveData['Post'] = $data; + + $this->Post->create(); + $saveResult = $this->Post->save($saveData); + $this->assertTrue(!empty($saveResult) && is_array($saveResult)); + + $result = $this->Post->find('all'); + + $this->assertEqual(1, count($result)); + $resultData = $result[0]['Post']; + + $this->assertEqual(7, count($resultData)); + $this->assertTrue($resultData['count'] === 4); + $this->assertFalse($resultData['count'] === '4'); + + $data = array( + 'title' => 'test', + 'body' => 'aaaa', + 'text' => 'bbbb', + 'count' => '4' // force type cast by schema + ); + $saveData['Post'] = $data; + + $this->Post->create(); + $saveResult = $this->Post->save($saveData); + $this->assertTrue(!empty($saveResult) && is_array($saveResult)); + + $result = $this->Post->find('all'); + + $this->assertEqual(2, count($result)); + $resultData = $result[1]['Post']; + + $this->assertEqual(7, count($resultData)); + $this->assertTrue($resultData['count'] === 4); + $this->assertFalse($resultData['count'] === '4'); + } + + /** * Tests insertId after saving * From 5950554bca345c3bd4afa7424ef51d2e405d39f9 Mon Sep 17 00:00:00 2001 From: Aaron Peterson Date: Sat, 22 Oct 2011 12:24:38 -0700 Subject: [PATCH 2/2] lets cake Model class handle type juggling for dates/times --- Model/Datasource/MongodbSource.php | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/Model/Datasource/MongodbSource.php b/Model/Datasource/MongodbSource.php index 8fa6348..f55092f 100644 --- a/Model/Datasource/MongodbSource.php +++ b/Model/Datasource/MongodbSource.php @@ -1154,26 +1154,22 @@ public function mapReduce($query, $timeout = null) { /** - * Prepares a value, or an array of values for database queries by quoting and escaping them. + * Prepares a value + * + * From CakePHP: "Returns a quoted and escaped string of $data for use in an SQL statement." + * + * For MongodbSource: Casts type into that defined in the schema using appropriate formatter * * @param mixed $data A value or an array of values to prepare. - * @param string $column The column into which this data will be inserted - * @param boolean $read Value to be used in READ or WRITE context + * @param string $column The column data type * @return mixed Prepared value or array of values. * @access public */ public function value($data, $column = null) { - /* - $return = parent::value($data, $column); - if ($return === null && $data !== null) { - return $data; + $ignore = array('date', 'datetime', 'timestamp', 'time'); // Model::save only juggles date types? + if (!in_array($column, $ignore) && isset($this->columns[$column]['formatter'])) { + return $this->columns[$column]['formatter']($data); } - * - */ - if ($column == 'integer') { - return intval($data); - } - return $data; }