1
0
mirror of synced 2024-12-13 06:46:03 +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));
$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.")";
$stmt = $this->dbh->prepare($sql);

View File

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

View File

@ -250,7 +250,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @return integer
*/
private function cleanData() {
$tmp = $this->data;
$tmp = $this->data;
$this->data = array();
@ -259,7 +259,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
foreach($this->table->getColumnNames() as $name) {
$type = $this->table->getTypeOf($name);
if( ! isset($tmp[$name])) {
$lower = strtolower($name);
if( ! isset($tmp[$lower])) {
//if($type == 'array') {
// $this->data[$name] = array();
//} else
@ -269,23 +271,23 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
case "array":
case "object":
if($tmp[$name] !== self::$null) {
if(is_string($tmp[$name])) {
$value = unserialize($tmp[$name]);
if($tmp[$lower] !== self::$null) {
if(is_string($tmp[$lower])) {
$value = unserialize($tmp[$lower]);
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
$value = $tmp[$name];
$value = $tmp[$lower];
$this->data[$name] = $value;
}
break;
case "enum":
$this->data[$name] = $this->table->enumValue($name, $tmp[$name]);
$this->data[$name] = $this->table->enumValue($name, $tmp[$lower]);
break;
default:
$this->data[$name] = $tmp[$name];
$this->data[$name] = $tmp[$lower];
endswitch;
$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())." = ?";
$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->cleanData();
@ -595,6 +602,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* rawSet
* 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 $value value of the property or reference
*/

View File

@ -63,7 +63,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
*/
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;
/**
@ -128,6 +128,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
/**
* the constructor
* @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.");
}
$record->setUp();
// save parents
@ -285,6 +287,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$this->primaryKeys[] = $name;
}
}
/**
* @return mixed
*/
@ -772,6 +775,8 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
* @return Doctrine_Record
*/
public function getRecord() {
$this->data = array_change_key_case($this->data, CASE_LOWER);
$key = $this->getIdentifier();
if( ! is_array($key))

View File

@ -7,7 +7,7 @@ Doctrine::autoload('Doctrine_Exception');
class Doctrine_Table_Exception extends Doctrine_Exception {
public function __construct() {
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);
}
}

View File

@ -2,10 +2,12 @@
require_once("UnitTestCase.php");
class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
public function prepareTables() {
$this->tables[] = "enumTest";
parent::prepareTables();
}
public function testReferences2() {
$user = new User();
@ -137,13 +139,40 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$date = new DateTest();
}
}
public function testEnumType() {
$enum = new EnumTest();
$enum->status = "open";
$this->assertEqual($enum->status, "open");
$enum->save();
$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();
$this->assertEqual($enum->status, "open");
@ -157,7 +186,19 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$enum->refresh();
$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() {
$user = $this->connection->getTable("User")->find(4);
$str = serialize($user);

View File

@ -1,6 +1,58 @@
<?php
require_once("UnitTestCase.php");
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() {
$table = $this->connection->getTable("User");
}
@ -70,8 +122,8 @@ class Doctrine_TableTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users->count(), 8);
$this->assertTrue($users instanceof Doctrine_Collection);
}
public function testFindBySql() {
$users = $this->objTable->findBySql("name LIKE '%Arnold%'");
public function testFindByDql() {
$users = $this->objTable->findByDql("name LIKE '%Arnold%'");
$this->assertEqual($users->count(), 1);
$this->assertTrue($users instanceof Doctrine_Collection);
}

View File

@ -17,6 +17,18 @@ class Entity extends Doctrine_Record {
$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 {
public function setTableDefinition() {
$this->hasColumn("entity1","integer");

View File

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