diff --git a/cake/libs/model/datasources/dbo/dbo_postgres.php b/cake/libs/model/datasources/dbo/dbo_postgres.php index ed32e74446b..9d1558e3371 100644 --- a/cake/libs/model/datasources/dbo/dbo_postgres.php +++ b/cake/libs/model/datasources/dbo/dbo_postgres.php @@ -265,6 +265,9 @@ function &describe(&$model) { $this->_sequenceMap[$table][$c['name']] = $seq[1]; } } + if ($fields[$c['name']]['type'] == 'boolean' && !empty($fields[$c['name']]['default'])) { + $fields[$c['name']]['default'] = constant($fields[$c['name']]['default']); + } } } $this->__cacheDescription($table, $fields); diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 190a3660cfc..8906676fdf1 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1086,11 +1086,11 @@ function create($data = array(), $filterKey = false) { if ($data !== null && $data !== false) { foreach ($this->schema() as $field => $properties) { - if ($this->primaryKey !== $field && isset($properties['default'])) { + if ($this->primaryKey !== $field && isset($properties['default']) && $properties['default'] !== '') { $defaults[$field] = $properties['default']; } } - $this->set(Set::filter($defaults)); + $this->set($defaults); $this->set($data); } if ($filterKey) { diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php index 33ed58df5d1..d9c6d023cda 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php @@ -220,6 +220,7 @@ class DboPostgresTest extends CakeTestCase { */ var $fixtures = array('core.user', 'core.binary_test', 'core.comment', 'core.article', 'core.tag', 'core.articles_tag', 'core.attachment', 'core.person', 'core.post', 'core.author', + 'core.datatype', ); /** * Actual DB connection used in testing @@ -437,6 +438,17 @@ function testBooleanNormalization() { $this->assertFalse($this->db2->boolean('')); } +/** + * test that default -> false in schemas works correctly. + * + * @return void + */ + function testBooleanDefaultFalseInSchema() { + $model = new Model(array('name' => 'Datatype', 'table' => 'datatypes', 'ds' => 'test_suite')); + $model->create(); + $this->assertIdentical(false, $model->data['Datatype']['bool']); + } + /** * testLastInsertIdMultipleInsert method * @@ -446,23 +458,15 @@ function testBooleanNormalization() { function testLastInsertIdMultipleInsert() { $db1 = ConnectionManager::getDataSource('test_suite'); - if (PHP5) { - $db2 = clone $db1; - } else { - $db2 = $db1; - } - - $db2->connect(); - $this->assertNotEqual($db1->connection, $db2->connection); - $table = $db1->fullTableName('users', false); $password = '5f4dcc3b5aa765d61d8327deb882cf99'; $db1->execute( "INSERT INTO {$table} (\"user\", password) VALUES ('mariano', '{$password}')" ); - $db2->execute("INSERT INTO {$table} (\"user\", password) VALUES ('hoge', '{$password}')"); $this->assertEqual($db1->lastInsertId($table), 1); - $this->assertEqual($db2->lastInsertId($table), 2); + + $db1->execute("INSERT INTO {$table} (\"user\", password) VALUES ('hoge', '{$password}')"); + $this->assertEqual($db1->lastInsertId($table), 2); } /** @@ -582,7 +586,7 @@ function testCakeSchema() { $db1 =& ConnectionManager::getDataSource('test_suite'); $db1->cacheSources = false; $db1->reconnect(array('persistent' => false)); - $db1->query('CREATE TABLE ' . $db1->fullTableName('datatypes') . ' ( + $db1->query('CREATE TABLE ' . $db1->fullTableName('datatype_tests') . ' ( id serial NOT NULL, "varchar" character varying(40) NOT NULL, "full_length" character varying NOT NULL, @@ -590,31 +594,30 @@ function testCakeSchema() { date date, CONSTRAINT test_suite_data_types_pkey PRIMARY KEY (id) )'); - $model = new Model(array('name' => 'Datatype', 'ds' => 'test_suite')); + $model = new Model(array('name' => 'DatatypeTest', 'ds' => 'test_suite')); $schema = new CakeSchema(array('connection' => 'test_suite')); $result = $schema->read(array( 'connection' => 'test_suite', - 'models' => array('Datatype') )); - $schema->tables = array('datatypes' => $result['tables']['datatypes']); - $result = $db1->createSchema($schema, 'datatypes'); + $schema->tables = array('datatype_tests' => $result['tables']['missing']['datatype_tests']); + $result = $db1->createSchema($schema, 'datatype_tests'); $this->assertNoPattern('/timestamp DEFAULT/', $result); $this->assertPattern('/\"full_length\"\s*text\s.*,/', $result); $this->assertPattern('/timestamp\s*,/', $result); - $db1->query('DROP TABLE ' . $db1->fullTableName('datatypes')); + $db1->query('DROP TABLE ' . $db1->fullTableName('datatype_tests')); $db1->query($result); $result2 = $schema->read(array( 'connection' => 'test_suite', - 'models' => array('Datatype') + 'models' => array('DatatypeTest') )); - $schema->tables = array('datatypes' => $result2['tables']['datatypes']); - $result2 = $db1->createSchema($schema, 'datatypes'); + $schema->tables = array('datatype_tests' => $result2['tables']['missing']['datatype_tests']); + $result2 = $db1->createSchema($schema, 'datatype_tests'); $this->assertEqual($result, $result2); - $db1->query('DROP TABLE ' . $db1->fullTableName('datatypes')); + $db1->query('DROP TABLE ' . $db1->fullTableName('datatype_tests')); } /** diff --git a/cake/tests/fixtures/datatype_fixture.php b/cake/tests/fixtures/datatype_fixture.php index 7df574b27a8..a12335cd371 100644 --- a/cake/tests/fixtures/datatype_fixture.php +++ b/cake/tests/fixtures/datatype_fixture.php @@ -43,6 +43,7 @@ class DatatypeFixture extends CakeTestFixture { var $fields = array( 'id' => array('type' => 'integer', 'null'=> false, 'default'=> 0, 'key' => 'primary'), 'float_field' => array('type' => 'float', 'length' => '5,2', 'null' => false, 'default' => null), + 'bool' => array('type' => 'boolean', 'null' => false, 'default' => false), ); /** @@ -52,6 +53,6 @@ class DatatypeFixture extends CakeTestFixture { * @access public */ var $records = array( - array('id' => 1, 'float_field' => 42.23), + array('id' => 1, 'float_field' => 42.23, 'bool' => false), ); }