Skip to content

Commit

Permalink
Implementing auto casting from correct type for fields in schema when
Browse files Browse the repository at this point in the history
using Tabel::find()
  • Loading branch information
lorenzo committed Apr 21, 2013
1 parent 8f13763 commit fca4459
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
10 changes: 10 additions & 0 deletions lib/Cake/ORM/Query.php
Expand Up @@ -13,6 +13,7 @@ public function repository(Table $table = null) {
return $this->_table;
}
$this->_table = $table;
$this->_addDefaultTypes($table);
return $this;
}

Expand Down Expand Up @@ -65,4 +66,13 @@ protected function _aliasFields() {
$this->select($aliased, true);
}

protected function _addDefaultTypes(Table $table) {
$alias = $table->alias();
$fields = [];
foreach ($table->schema() as $f => $meta) {
$fields[$f] = $fields[$alias . '.' . $f] = $meta['type'];
}
$this->defaultTypes($this->defaultTypes() + $fields);
}

}
60 changes: 57 additions & 3 deletions lib/Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -32,14 +32,15 @@ public function setUp() {

public function tearDown() {
$this->connection->execute('DROP TABLE IF EXISTS things');
$this->connection->execute('DROP TABLE IF EXISTS dates');
}

/**
* Auxiliary function to insert a couple rows in a newly created table
*
* @return void
**/
protected function _createTables() {
protected function _createThingsTable() {
$table = 'CREATE TEMPORARY TABLE things(id int, title varchar(20), body varchar(50))';
$this->connection->execute($table);
$data = ['id' => '1', 'title' => 'a title', 'body' => 'a body'];
Expand All @@ -55,8 +56,43 @@ protected function _createTables() {
$result->execute();
}

/**
* Auxiliary function to insert a couple rows in a newly created table containing dates
*
* @return void
**/
protected function _createDatesTable() {
$table = 'CREATE TEMPORARY TABLE dates(id int, name varchar(50), posted timestamp, visible char(1))';
$this->connection->execute($table);
$data = [
'id' => '1',
'name' => 'Chuck Norris',
'posted' => new \DateTime('2012-12-21 12:00'),
'visible' => 'Y'
];
$result = $this->connection->insert(
'dates',
$data,
['id' => 'integer', 'name' => 'string', 'posted' => 'datetime', 'visible' => 'string']
);

$result->bindValue(1, '2', 'integer');
$result->bindValue(2, 'Bruce Lee');
$result->bindValue(3, new \DateTime('2012-12-22 12:00'), 'datetime');
$result->bindValue(4, 'N');
$result->execute();

$result->bindValue(1, 3, 'integer');
$result->bindValue(2, 'Jet Li');
$result->bindValue(3, new \DateTime('2012-12-25 12:00'), 'datetime');
$result->bindValue(4, null);
$result->execute();

return $result;
}

public function testFindAllNoFields() {
$this->_createTables();
$this->_createThingsTable();
$table = new Table(['name' => 'things', 'connection' => $this->connection]);
$results = $table->find('all')->toArray();
$expected = [
Expand All @@ -67,7 +103,7 @@ public function testFindAllNoFields() {
}

public function testFindAllSomeFields() {
$this->_createTables();
$this->_createThingsTable();
$table = new Table(['name' => 'things', 'connection' => $this->connection]);
$results = $table->find('all')->select(['id', 'title'])->toArray();
$expected = [
Expand All @@ -84,4 +120,22 @@ public function testFindAllSomeFields() {
$this->assertSame($expected, $results);
}

public function testFindAllConditionAutoTypes() {
$this->_createDatesTable();
$table = new Table(['name' => 'dates', 'connection' => $this->connection]);
$query = $table->find('all')
->select(['id', 'name'])
->where(['posted >=' => new \DateTime('2012-12-22 12:01')]);
$expected = [
['dates' => ['id' => 3, 'name' => 'Jet Li']]
];
$this->assertSame($expected, $query->toArray());

$query->orWhere(['dates.posted' => new \DateTime('2012-12-22 12:00')]);
$expected = [
['dates' => ['id' => 2, 'name' => 'Bruce Lee']],
['dates' => ['id' => 3, 'name' => 'Jet Li']]
];
$this->assertSame($expected, $query->toArray());
}
}

0 comments on commit fca4459

Please sign in to comment.