Skip to content
Permalink
Browse files

Adapting current code to the new SchemaCollection class

  • Loading branch information...
lorenzo committed Jun 2, 2013
1 parent 321ac3f commit 4bc7cc4bf98e3508261e879cc3bbf17eccbe01c8
@@ -37,6 +37,7 @@ class Type {
'binary' => 'Cake\Database\Type\BinaryType',
'date' => 'Cake\Database\Type\DateType',
'datetime' => 'Cake\Database\Type\DateTimeType',
'timestamp' => 'Cake\Database\Type\DateTimeType',
'time' => 'Cake\Database\Type\TimeType'
];
@@ -377,7 +377,7 @@ public function attachTo(Query $query, array $options = []) {
if (empty($options['fields'])) {
$f = isset($options['fields']) ? $options['fields'] : null;
if ($options['includeFields'] && ($f === null || $f !== false)) {
$options['fields'] = array_keys($target->schema());
$options['fields'] = $target->schema()->columns();
}
}
@@ -37,9 +37,10 @@ public function repository(Table $table = null) {
public function addDefaultTypes(Table $table) {
$alias = $table->alias();
$schema = $table->schema();
$fields = [];
foreach ($table->schema() as $f => $meta) {
$fields[$f] = $fields[$alias . '.' . $f] = $meta['type'];
foreach ($schema->columns() as $f) {
$fields[$f] = $fields[$alias . '.' . $f] = $schema->column($f)['type'];
}
$this->defaultTypes($this->defaultTypes() + $fields);
}
@@ -283,7 +284,7 @@ protected function _addDefaultFields() {
if (!count($select)) {
$this->_hasFields = false;
$this->select(array_keys($this->repository()->schema()));
$this->select($this->repository()->schema()->columns());
$select = $this->clause('select');
}
@@ -143,18 +143,16 @@ protected function _castValues($table, $values) {
$alias = $table->alias();
$driver = $this->_query->connection()->driver();
if (empty($this->types[$alias])) {
$this->types[$alias] = array_map(function($f) {
return $f['type'];
}, $table->schema());
$schema = $table->schema();
foreach ($schema->columns() as $col) {
$this->types[$alias][$col] = Type::build($schema->column($col)['type']);
}
}
foreach ($values as $field => $value) {
if (!isset($this->types[$alias][$field])) {
continue;
}
if (is_string($this->types[$alias][$field])) {
$this->types[$alias][$field] = Type::build($this->types[$alias][$field]);
}
$values[$field] = $this->types[$alias][$field]->toPHP($value, $driver);
}
@@ -21,6 +21,7 @@
use Cake\ORM\Association\HasMany;
use Cake\ORM\Association\BelongsToMany;
use Cake\Utility\Inflector;
use Cake\Database\Schema\Table as Schema;
class Table {
@@ -144,10 +145,15 @@ public function connection($conn = null) {
public function schema($schema = null) {
if ($schema === null) {
if ($this->_schema === null) {
$this->_schema = $this->connection()->describe($this->_table);
$this->_schema = $this->connection()
->schemaCollection()
->describe($this->_table);
}
return $this->_schema;
}
if (is_array($schema)) {
$schema = new Schema($this->table(), $schema);
}
return $this->_schema = $schema;
}
@@ -124,7 +124,7 @@ public function testConfigAndBuild() {
$this->assertEquals('things', $table->table());
$this->assertEquals('foo', $table->alias());
$this->assertSame($this->connection, $table->connection());
$this->assertEquals($schema, $table->schema());
$this->assertEquals(array_keys($schema), $table->schema()->columns());
Table::clearRegistry();
$this->assertEmpty(Table::config());
@@ -136,7 +136,7 @@ public function testConfigAndBuild() {
$this->assertEquals('dates', $table->table());
$this->assertEquals('foo', $table->alias());
$this->assertSame($this->connection, $table->connection());
$this->assertEquals($schema, $table->schema());
$this->assertEquals(array_keys($schema), $table->schema()->columns());
}
public function testInstance() {

0 comments on commit 4bc7cc4

Please sign in to comment.
You can’t perform that action at this time.