1
0
mirror of synced 2025-01-18 06:21:40 +03:00

Fixed default value support + camelCase column problems

This commit is contained in:
zYne 2006-09-17 15:44:10 +00:00
parent 75284895d3
commit ddbf6c949b
4 changed files with 48 additions and 37 deletions

View File

@ -57,8 +57,8 @@ class Doctrine_Locking_Manager_Pessimistic
if($this->_dataSource->getAttribute(Doctrine::ATTR_CREATE_TABLES) === true)
{
$columns = array();
$columns['object_type'] = array('string', 50, array('notnull' => true, 'primary'));
$columns['object_key'] = array('string', 250, array('notnull' => true, 'primary'));
$columns['object_type'] = array('string', 50, array('notnull' => true, 'primary' => true));
$columns['object_key'] = array('string', 250, array('notnull' => true, 'primary' => true));
$columns['user_ident'] = array('string', 50, array('notnull' => true));
$columns['timestamp_obtained'] = array('integer', 10, array('notnull' => true));

View File

@ -253,8 +253,11 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
if($default === null)
$default = self::$null;
if($value === self::$null || $overwrite)
if($value === self::$null || $overwrite) {
$this->data[$column] = $default;
$this->modified[] = $column;
$this->state = Doctrine_Record::STATE_TDIRTY;
}
}
}
/**
@ -281,35 +284,31 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
foreach($this->table->getColumnNames() as $name) {
$type = $this->table->getTypeOf($name);
$lower = strtolower($name);
if( ! isset($tmp[$lower])) {
//if($type == 'array') {
// $this->data[$name] = array();
//} else
$this->data[$name] = self::$null;
if( ! isset($tmp[$name])) {
$this->data[$name] = self::$null;
} else {
switch($type):
case "array":
case "object":
if($tmp[$lower] !== self::$null) {
if(is_string($tmp[$lower])) {
$value = unserialize($tmp[$lower]);
if($tmp[$name] !== self::$null) {
if(is_string($tmp[$name])) {
$value = unserialize($tmp[$name]);
if($value === false)
throw new Doctrine_Record_Exception("Unserialization of $name failed. ".var_dump($tmp[$lower],true));
} else
$value = $tmp[$lower];
$value = $tmp[$name];
$this->data[$name] = $value;
}
break;
case "enum":
$this->data[$name] = $this->table->enumValue($name, $tmp[$lower]);
$this->data[$name] = $this->table->enumValue($name, $tmp[$name]);
break;
default:
$this->data[$name] = $tmp[$lower];
$this->data[$name] = $tmp[$name];
endswitch;
$count++;
}
@ -601,36 +600,34 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
*/
public function get($name, $invoke = true) {
$listener = $this->table->getAttribute(Doctrine::ATTR_LISTENER);
$value = self::$null;
$lower = strtolower($name);
if(isset($this->data[$name])) {
if(isset($this->data[$lower])) {
// check if the property is null (= it is the Doctrine_Null object located in self::$null)
if($this->data[$name] === self::$null) {
if($this->data[$lower] === self::$null) {
$this->load();
}
if($this->data[$name] === self::$null)
if($this->data[$lower] === self::$null)
$value = null;
else
$value = $this->data[$name];
$value = $this->data[$lower];
}
if($value !== self::$null) {
if($invoke && $name !== $this->table->getIdentifier()) {
return $this->table->getAttribute(Doctrine::ATTR_LISTENER)->onGetProperty($this, $name, $value);
} else
return $value;
}
if(isset($this->id[$name]))
return $this->id[$name];
if(isset($this->id[$lower]))
return $this->id[$lower];
if($name === $this->table->getIdentifier())
return null;
@ -706,7 +703,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @return void
*/
public function set($name,$value) {
if(isset($this->data[$name])) {
$lower = strtolower($name);
if(isset($this->data[$lower])) {
if($value instanceof Doctrine_Record) {
$id = $value->getIncremented();
@ -715,7 +714,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$value = $id;
}
$old = $this->get($name, false);
$old = $this->get($lower, false);
if($old !== $value) {
@ -725,8 +724,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
if($value === null)
$value = self::$null;
$this->data[$name] = $value;
$this->modified[] = $name;
$this->data[$lower] = $value;
$this->modified[] = $lower;
switch($this->state):
case Doctrine_Record::STATE_CLEAN:
case Doctrine_Record::STATE_PROXY:

View File

@ -291,7 +291,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
unset($options[$k]);
}
}
$name = strtolower($name);
$this->columns[$name] = array($type,$length,$options);
if(isset($options['primary'])) {
@ -318,6 +318,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
* @return mixed
*/
public function getDefaultValueOf($column) {
$column = strtolower($column);
if( ! isset($this->columns[$column]))
throw new Doctrine_Table_Exception("Couldn't get default value. Column ".$column." doesn't exist.");
@ -680,7 +681,11 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$this->relations[$alias] = $relation;
return $this->relations[$alias];
}
throw new Doctrine_Table_Exception($this->name . " doesn't have a relation to " . $original);
try {
throw new Doctrine_Table_Exception($this->name . " doesn't have a relation to " . $original);
} catch(Exception $e) {
print $e;
}
}
/**
* returns an array containing all foreign key objects
@ -922,7 +927,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
* @return void
*/
final public function setEnumValues($field, array $values) {
$this->enum[$field] = $values;
$this->enum[strtolower($field)] = $values;
}
/**
* @param string $field
@ -936,12 +941,20 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
}
/**
* enumValue
*
* @param string $field
* @param integer $index
* @return mixed
*/
final public function enumValue($field, $index) {
return isset($this->enum[$field][$index])?$this->enum[$field][$index]:$index;
}
/**
* enumIndex
*
* @param string $field
* @param mixed $value
* @return mixed
*/
final public function enumIndex($field, $value) {
if( ! isset($this->enum[$field]))

View File

@ -5,13 +5,10 @@ class Doctrine_TableTestCase extends Doctrine_UnitTestCase {
$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';
@ -55,6 +52,7 @@ class Doctrine_TableTestCase extends Doctrine_UnitTestCase {
$this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
}
public function testBind() {
$table = $this->connection->getTable("User");
}
@ -150,5 +148,6 @@ class Doctrine_TableTestCase extends Doctrine_UnitTestCase {
public function testApplyInheritance() {
$this->assertEqual($this->objTable->applyInheritance("id = 3"), "id = 3 AND type = ?");
}
}
?>