From e3f5aa9fb2b18a1ad3052fb28ade7d4629ec1a68 Mon Sep 17 00:00:00 2001 From: zYne Date: Tue, 26 Sep 2006 21:12:14 +0000 Subject: [PATCH] Boolean type bugs fixed, fixes #101 Ticket: 101 --- lib/Doctrine/Hydrate.php | 13 +++++ lib/Doctrine/Query/Where.php | 7 ++- lib/Doctrine/Record.php | 7 +++ ...le definition - Data types and lengths.php | 3 +- manual/documentation.php | 47 +++++++------------ tests/BooleanTestCase.php | 28 +++++++++-- tests/DataDictSqliteTestCase.php | 2 +- tests/run.php | 16 ++++--- 8 files changed, 78 insertions(+), 45 deletions(-) diff --git a/lib/Doctrine/Hydrate.php b/lib/Doctrine/Hydrate.php index 8064a7fdf..59c42f032 100644 --- a/lib/Doctrine/Hydrate.php +++ b/lib/Doctrine/Hydrate.php @@ -235,6 +235,17 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { return array(); } + /** + * convertBoolean + * converts boolean to integers + * + * @param mixed $item + * @return void + */ + public static function convertBoolean(&$item) { + if(is_bool($item)) + $item = (int) $item; + } /** * execute * executes the dql query and populates all collections @@ -246,6 +257,8 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { $this->data = array(); $this->collections = array(); + array_walk($params, array(__CLASS__, 'convertBoolean')); + if( ! $this->view) $query = $this->getQuery(); else diff --git a/lib/Doctrine/Query/Where.php b/lib/Doctrine/Query/Where.php index 00238844d..98121c74e 100644 --- a/lib/Doctrine/Query/Where.php +++ b/lib/Doctrine/Query/Where.php @@ -75,13 +75,18 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition { $enumIndex = $table->enumIndex($field, trim($value,"'")); $alias = $this->query->getTableAlias($reference); $table = $this->query->getTable($alias); + + if(trim($value) == 'true') + $value = 1; + elseif(trim($value) == 'false') + $value = 0; switch($operator) { case '<': case '>': case '=': if($enumIndex !== false) - $value = $enumIndex; + $value = $enumIndex; $where = $alias.'.'.$field.' '.$operator.' '.$value; break; diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index c06240268..680b42613 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -324,6 +324,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite case "enum": $this->data[$name] = $this->table->enumValue($name, $tmp[$name]); break; + case "boolean": + case "integer": + if($tmp[$name] !== self::$null) + settype($tmp[$name], $type); default: $this->data[$name] = $tmp[$name]; endswitch; @@ -889,6 +893,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite case 'gzip': $a[$v] = gzcompress($this->data[$v],5); break; + case 'boolean': + $a[$v] = (int) $this->data[$v]; + break; case 'enum': $a[$v] = $this->table->enumIndex($v,$this->data[$v]); break; diff --git a/manual/docs/Getting started - Setting table definition - Data types and lengths.php b/manual/docs/Getting started - Setting table definition - Data types and lengths.php index 97b5772ac..f90719b15 100644 --- a/manual/docs/Getting started - Setting table definition - Data types and lengths.php +++ b/manual/docs/Getting started - Setting table definition - Data types and lengths.php @@ -13,8 +13,7 @@ Following data types are availible in doctrine:
  • object
  • enum - +
  • timestamp
    Database 'timestamp' type
  • clob diff --git a/manual/documentation.php b/manual/documentation.php index 36bcd58c0..ed28144a7 100644 --- a/manual/documentation.php +++ b/manual/documentation.php @@ -102,6 +102,16 @@ $menu = array("Getting started" => "Enum emulation", ), + + "Record identifiers" => array( + "Introduction", + "Autoincremented", + "Natural", + "Composite", + "Sequential") + ), + "Schema reference" => + array( "Data types" => array( "Boolean", "Integer", @@ -116,26 +126,7 @@ $menu = array("Getting started" => "Enum", "Gzip", ), - "Record identifiers" => array( - "Introduction", - "Autoincremented", - "Natural", - "Composite", - "Sequential") - ), - "Schema reference" => - array( - "Data types" => array( - "PHP based types" => - array( - "Boolean", - "Integer", - "Float", - "String", - "Array", - "Object", - ), - ), + ), "Basic Components" => array( @@ -153,6 +144,7 @@ $menu = array("Getting started" => "Getting record state", "Getting object copy", "Serializing", + "Existence checking", "Callbacks"), "Connection" => array("Introduction", @@ -199,6 +191,10 @@ $menu = array("Getting started" => "Using SQL", "Adding components", "Method overloading"), + "DB" => array( + "Introduction", + "Connecting to a database", + "Using event listeners"), /** "Statement - UNDER CONSTRUCTION" => array("Introduction", "Setting parameters", @@ -263,6 +259,8 @@ $menu = array("Getting started" => "List of events", "Listening events", "Chaining", + "AccessorInvoker", + "Creating a logger", ), "Validators" => array( "Intruduction", @@ -275,15 +273,6 @@ $menu = array("Getting started" => "Managing views", "Using views" ), - /** - "Hook" => array( - "Introduction", - "Parameter hooking", - "Paging", - "Setting conditions", - "Sorting" - ), - */ "Cache" => array( "Introduction", "Query cache"), diff --git a/tests/BooleanTestCase.php b/tests/BooleanTestCase.php index e20a76378..953fc28b2 100644 --- a/tests/BooleanTestCase.php +++ b/tests/BooleanTestCase.php @@ -31,15 +31,33 @@ class Doctrine_BooleanTestCase extends Doctrine_UnitTestCase { $test = $test->getTable()->find($test->id); $this->assertEqual($test->is_working, true); } - - public function testFetching() { - + public function testNormalQuerying() { $query = new Doctrine_Query($this->connection); - $ret = $query->query('FROM BooleanTest WHERE BooleanTest.is_working = ?', array(false)); + $ret = $query->query('FROM BooleanTest WHERE BooleanTest.is_working = 0'); $this->assertEqual(count($ret), 1); $query = new Doctrine_Query($this->connection); - $ret = $query->query('FROM BooleanTest WHERE BooleanTest.is_working = ?', array(true)); + $ret = $query->query('FROM BooleanTest WHERE BooleanTest.is_working = 1'); + Doctrine_Lib::formatSql($query->getQuery()); + $this->assertEqual(count($ret), 1); + } + public function testPreparedQueries() { + $query = new Doctrine_Query($this->connection); + $ret = $query->query('FROM BooleanTest WHERE BooleanTest.is_working = ?', array(false)); + $this->assertEqual(count($ret), 1); + + $query = new Doctrine_Query($this->connection); + $ret = $query->query('FROM BooleanTest WHERE BooleanTest.is_working = ?', array(true)); + $this->assertEqual(count($ret), 1); + } + public function testFetchingWithSmartConversion() { + $query = new Doctrine_Query($this->connection); + $ret = $query->query('FROM BooleanTest WHERE BooleanTest.is_working = false'); + $this->assertEqual(count($ret), 1); + + $query = new Doctrine_Query($this->connection); + $ret = $query->query('FROM BooleanTest WHERE BooleanTest.is_working = true'); + Doctrine_Lib::formatSql($query->getQuery()); $this->assertEqual(count($ret), 1); } diff --git a/tests/DataDictSqliteTestCase.php b/tests/DataDictSqliteTestCase.php index 3ce6ddb05..0c6ad934e 100644 --- a/tests/DataDictSqliteTestCase.php +++ b/tests/DataDictSqliteTestCase.php @@ -16,7 +16,7 @@ class Doctrine_DataDict_Sqlite_TestCase extends Doctrine_UnitTestCase { } public function testListTables() { $result = $this->dict->listTables(); - + } public function testIntegerType() { $this->assertEqual($this->columns['col_int']->isUnique(), false); diff --git a/tests/run.php b/tests/run.php index b1bed63eb..6c672d010 100644 --- a/tests/run.php +++ b/tests/run.php @@ -32,12 +32,11 @@ require_once("ImportTestCase.php"); require_once("BooleanTestCase.php"); require_once("EnumTestCase.php"); require_once("RelationAccessTestCase.php"); - +require_once("DataDictSqliteTestCase.php"); error_reporting(E_ALL); $test = new GroupTest("Doctrine Framework Unit Tests"); - $test->addTestCase(new Doctrine_DB_TestCase()); $test->addTestCase(new Doctrine_ConnectionTestCase()); @@ -76,9 +75,9 @@ $test->addTestCase(new Doctrine_RawSql_TestCase()); $test->addTestCase(new Doctrine_Query_Limit_TestCase()); -$test->addTestCase(new Doctrine_SchemaTestCase()); +//$test->addTestCase(new Doctrine_SchemaTestCase()); -$test->addTestCase(new Doctrine_ImportTestCase()); +//$test->addTestCase(new Doctrine_ImportTestCase()); $test->addTestCase(new Doctrine_CollectionTestCase()); @@ -93,6 +92,9 @@ $test->addTestCase(new Doctrine_EnumTestCase()); $test->addTestCase(new Doctrine_RelationAccessTestCase()); $test->addTestCase(new Doctrine_EventListener_Chain_TestCase()); + +$test->addTestCase(new Doctrine_DataDict_Sqlite_TestCase()); + //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase()); @@ -124,9 +126,9 @@ if (TextReporter::inCli()) { } else { if (isset($_POST)) { - $dsn = $_POST['dsn']; - $username = $_POST['username']; - $password = $_POST['password']; + $dsn = isset($_POST['dsn'])?$_POST['dsn']:null; + $username = isset($_POST['username'])?$_POST['username']:null; + $password = isset($_POST['password'])?$_POST['password']:null; } $test->run(new MyReporter()); $output = ob_get_clean();