1
0
mirror of synced 2024-12-13 14:56:01 +03:00

Case insensitive column names

This commit is contained in:
zYne 2006-09-03 19:20:02 +00:00
parent 9046569d68
commit 087de88f7c
9 changed files with 145 additions and 22 deletions

View File

@ -832,8 +832,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
} }
$strfields = join(", ", array_keys($array)); $strfields = join(", ", array_keys($array));
$strvalues = substr(str_repeat("?, ",count($array)),0,-2); $strvalues = substr(str_repeat("?, ",count($array)),0,-2);
$sql = "INSERT INTO ".$record->getTable()->getTableName()." (".$strfields.") VALUES (".$strvalues.")"; $sql = "INSERT INTO ".$record->getTable()->getTableName()." (".$strfields.") VALUES (".$strvalues.")";
$stmt = $this->dbh->prepare($sql); $stmt = $this->dbh->prepare($sql);

View File

@ -313,7 +313,6 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
foreach($data as $key => $row) { foreach($data as $key => $row) {
if(empty($row)) if(empty($row))
continue; continue;
$ids = $this->tables[$key]->getIdentifier(); $ids = $this->tables[$key]->getIdentifier();
@ -517,8 +516,11 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
foreach($data as $key => $value): foreach($data as $key => $value):
$e = explode("__",$key); $e = explode("__",$key);
$field = array_pop($e); $field = strtolower( array_pop($e) );
$component = implode("__",$e);
$component = strtolower( implode("__",$e) );
$data[$component][$field] = $value; $data[$component][$field] = $value;
unset($data[$key]); unset($data[$key]);

View File

@ -250,7 +250,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @return integer * @return integer
*/ */
private function cleanData() { private function cleanData() {
$tmp = $this->data; $tmp = $this->data;
$this->data = array(); $this->data = array();
@ -259,7 +259,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
foreach($this->table->getColumnNames() as $name) { foreach($this->table->getColumnNames() as $name) {
$type = $this->table->getTypeOf($name); $type = $this->table->getTypeOf($name);
if( ! isset($tmp[$name])) { $lower = strtolower($name);
if( ! isset($tmp[$lower])) {
//if($type == 'array') { //if($type == 'array') {
// $this->data[$name] = array(); // $this->data[$name] = array();
//} else //} else
@ -269,23 +271,23 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
case "array": case "array":
case "object": case "object":
if($tmp[$name] !== self::$null) { if($tmp[$lower] !== self::$null) {
if(is_string($tmp[$name])) { if(is_string($tmp[$lower])) {
$value = unserialize($tmp[$name]); $value = unserialize($tmp[$lower]);
if($value === false) if($value === false)
throw new Doctrine_Record_Exception("Unserialization of $name failed. ".var_dump($tmp[$name],true)); throw new Doctrine_Record_Exception("Unserialization of $name failed. ".var_dump($tmp[$lower],true));
} else } else
$value = $tmp[$name]; $value = $tmp[$lower];
$this->data[$name] = $value; $this->data[$name] = $value;
} }
break; break;
case "enum": case "enum":
$this->data[$name] = $this->table->enumValue($name, $tmp[$name]); $this->data[$name] = $this->table->enumValue($name, $tmp[$lower]);
break; break;
default: default:
$this->data[$name] = $tmp[$name]; $this->data[$name] = $tmp[$lower];
endswitch; endswitch;
$count++; $count++;
} }
@ -468,6 +470,11 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$query = $this->table->getQuery()." WHERE ".implode(" = ? AND ",$this->table->getPrimaryKeys())." = ?"; $query = $this->table->getQuery()." WHERE ".implode(" = ? AND ",$this->table->getPrimaryKeys())." = ?";
$this->data = $this->table->getConnection()->execute($query,$id)->fetch(PDO::FETCH_ASSOC); $this->data = $this->table->getConnection()->execute($query,$id)->fetch(PDO::FETCH_ASSOC);
if( ! $this->data)
throw new Doctrine_Record_Exception('Failed to refresh. Record does not exist anymore');
$this->data = array_change_key_case($this->data, CASE_LOWER);
$this->modified = array(); $this->modified = array();
$this->cleanData(); $this->cleanData();
@ -595,6 +602,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* rawSet * rawSet
* doctrine uses this function internally, not recommended for developers * doctrine uses this function internally, not recommended for developers
* *
* rawSet() works in very same same way as set() with an exception that
* 1. it cannot be used for setting references
* 2. it cannot load uninitialized fields
*
* @param mixed $name name of the property or reference * @param mixed $name name of the property or reference
* @param mixed $value value of the property or reference * @param mixed $value value of the property or reference
*/ */

View File

@ -63,7 +63,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
*/ */
private $query; private $query;
/** /**
* @var Doctrine_Connection $connection Doctrine_Connection object that created this table * @var Doctrine_Connection $connection Doctrine_Connection object that created this table
*/ */
private $connection; private $connection;
/** /**
@ -128,6 +128,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
/** /**
* the constructor * the constructor
* @throws Doctrine_ManagerException if there are no opened connections * @throws Doctrine_ManagerException if there are no opened connections
@ -231,6 +232,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
throw new Doctrine_Exception("Class '$name' has no table definition."); throw new Doctrine_Exception("Class '$name' has no table definition.");
} }
$record->setUp(); $record->setUp();
// save parents // save parents
@ -285,6 +287,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$this->primaryKeys[] = $name; $this->primaryKeys[] = $name;
} }
} }
/** /**
* @return mixed * @return mixed
*/ */
@ -772,6 +775,8 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
* @return Doctrine_Record * @return Doctrine_Record
*/ */
public function getRecord() { public function getRecord() {
$this->data = array_change_key_case($this->data, CASE_LOWER);
$key = $this->getIdentifier(); $key = $this->getIdentifier();
if( ! is_array($key)) if( ! is_array($key))

View File

@ -7,7 +7,7 @@ Doctrine::autoload('Doctrine_Exception');
class Doctrine_Table_Exception extends Doctrine_Exception { class Doctrine_Table_Exception extends Doctrine_Exception {
public function __construct() { public function __construct() {
parent::__construct("Couldn't initialize table. One instance of this parent::__construct("Couldn't initialize table. One instance of this
tabke already exists. Always use Doctrine_Session::getTable(\$name) table already exists. Always use Doctrine_Session::getTable(\$name)
to get on instance of a Doctrine_Table.",Doctrine::ERR_TABLE_INSTANCE); to get on instance of a Doctrine_Table.",Doctrine::ERR_TABLE_INSTANCE);
} }
} }

View File

@ -2,10 +2,12 @@
require_once("UnitTestCase.php"); require_once("UnitTestCase.php");
class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
public function prepareTables() { public function prepareTables() {
$this->tables[] = "enumTest"; $this->tables[] = "enumTest";
parent::prepareTables(); parent::prepareTables();
} }
public function testReferences2() { public function testReferences2() {
$user = new User(); $user = new User();
@ -137,13 +139,40 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$date = new DateTest(); $date = new DateTest();
} }
public function testEnumType() { public function testEnumType() {
$enum = new EnumTest(); $enum = new EnumTest();
$enum->status = "open"; $enum->status = "open";
$this->assertEqual($enum->status, "open"); $this->assertEqual($enum->status, "open");
$enum->save(); $enum->save();
$this->assertEqual($enum->status, "open"); $this->assertEqual($enum->status, "open");
$enum->refresh();
$this->assertEqual($enum->status, "open");
$enum->status = "closed";
$this->assertEqual($enum->status, "closed");
$enum->save();
$this->assertEqual($enum->status, "closed");
$enum->refresh();
$this->assertEqual($enum->status, "closed");
}
public function testEnumTypeWithCaseConversion() {
$this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
$enum = new EnumTest();
$enum->status = "open";
$this->assertEqual($enum->status, "open");
$enum->save();
$this->assertEqual($enum->status, "open");
$enum->refresh(); $enum->refresh();
$this->assertEqual($enum->status, "open"); $this->assertEqual($enum->status, "open");
@ -157,7 +186,19 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$enum->refresh(); $enum->refresh();
$this->assertEqual($enum->status, "closed"); $this->assertEqual($enum->status, "closed");
} }
public function testFailingRefresh() {
$enum = $this->connection->getTable('EnumTest')->find(1);
$this->dbh->query('DELETE FROM enum_test WHERE id = 1');
$f = false;
try {
$enum->refresh();
} catch(Doctrine_Record_Exception $e) {
$f = true;
}
$this->assertTrue($f);
}
public function testSerialize() { public function testSerialize() {
$user = $this->connection->getTable("User")->find(4); $user = $this->connection->getTable("User")->find(4);
$str = serialize($user); $str = serialize($user);

View File

@ -1,6 +1,58 @@
<?php <?php
require_once("UnitTestCase.php"); require_once("UnitTestCase.php");
class Doctrine_TableTestCase extends Doctrine_UnitTestCase { class Doctrine_TableTestCase extends Doctrine_UnitTestCase {
public function prepareTables() {
$this->tables[] = "FieldNameTest";
parent::prepareTables();
}
public function testFieldConversion() {
$this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
$user = $this->connection->getTable('User')->find(5);
$this->assertTrue($user instanceof User);
$t = new FieldNameTest();
$t->someColumn = 'abc';
$t->someEnum = 'php';
$t->someInt = 1;
$t->someArray = array();
$obj = new StdClass();
$t->someObject = $obj;
$this->assertEqual($t->someColumn, 'abc');
$this->assertEqual($t->someEnum, 'php');
$this->assertEqual($t->someInt, 1);
$this->assertEqual($t->someArray, array());
$this->assertEqual($t->someObject, $obj);
$t->save();
$this->assertEqual($t->someColumn, 'abc');
$this->assertEqual($t->someEnum, 'php');
$this->assertEqual($t->someInt, 1);
$this->assertEqual($t->someArray, array());
$this->assertEqual($t->someObject, $obj);
$t->refresh();
$this->assertEqual($t->someColumn, 'abc');
$this->assertEqual($t->someEnum, 'php');
$this->assertEqual($t->someInt, 1);
$this->assertEqual($t->someArray, array());
$this->assertEqual($t->someObject, $obj);
$this->connection->clear();
$t = $this->connection->getTable('FieldNameTest')->find(1);
$this->assertEqual($t->someColumn, 'abc');
$this->assertEqual($t->someEnum, 'php');
$this->assertEqual($t->someInt, 1);
$this->assertEqual($t->someArray, array());
$this->assertEqual($t->someObject, $obj);
}
public function testBind() { public function testBind() {
$table = $this->connection->getTable("User"); $table = $this->connection->getTable("User");
} }
@ -70,8 +122,8 @@ class Doctrine_TableTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users->count(), 8); $this->assertEqual($users->count(), 8);
$this->assertTrue($users instanceof Doctrine_Collection); $this->assertTrue($users instanceof Doctrine_Collection);
} }
public function testFindBySql() { public function testFindByDql() {
$users = $this->objTable->findBySql("name LIKE '%Arnold%'"); $users = $this->objTable->findByDql("name LIKE '%Arnold%'");
$this->assertEqual($users->count(), 1); $this->assertEqual($users->count(), 1);
$this->assertTrue($users instanceof Doctrine_Collection); $this->assertTrue($users instanceof Doctrine_Collection);
} }

View File

@ -17,6 +17,18 @@ class Entity extends Doctrine_Record {
$this->hasColumn("email_id","integer"); $this->hasColumn("email_id","integer");
} }
} }
class FieldNameTest extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("someColumn", "string", 200);
$this->hasColumn("someEnum", "enum", 4);
$this->hasColumn("someArray", "array", 100);
$this->hasColumn("someObject", "array", 200);
$this->hasColumn("someInt", "integer");
$this->setEnumValues("someEnum", array('php', 'java', 'python'));
}
}
class EntityReference extends Doctrine_Record { class EntityReference extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("entity1","integer"); $this->hasColumn("entity1","integer");

View File

@ -32,12 +32,12 @@ error_reporting(E_ALL);
$test = new GroupTest("Doctrine Framework Unit Tests"); $test = new GroupTest("Doctrine Framework Unit Tests");
$test->addTestCase(new Doctrine_TableTestCase());
$test->addTestCase(new Doctrine_RecordTestCase()); $test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_ConnectionTestCase()); $test->addTestCase(new Doctrine_ConnectionTestCase());
$test->addTestCase(new Doctrine_TableTestCase());
$test->addTestCase(new Doctrine_ManagerTestCase()); $test->addTestCase(new Doctrine_ManagerTestCase());
$test->addTestCase(new Doctrine_AccessTestCase()); $test->addTestCase(new Doctrine_AccessTestCase());
@ -75,6 +75,7 @@ $test->addTestCase(new Doctrine_SchemaTestCase());
$test->addTestCase(new Doctrine_ImportTestCase()); $test->addTestCase(new Doctrine_ImportTestCase());
$test->addTestCase(new Doctrine_CollectionTestCase()); $test->addTestCase(new Doctrine_CollectionTestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());