Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions Model/Datasource/MongodbSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down Expand Up @@ -1151,20 +1154,23 @@ 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, $read = true) {
$return = parent::value($data, $column, $read);
if ($return === null && $data !== null) {
return $data;
public function value($data, $column = null) {
$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);
}
return $return;
return $data;
}

/**
Expand Down
53 changes: 53 additions & 0 deletions Test/Case/Datasource/MongodbSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down