1
0
mirror of synced 2024-12-14 15:16:04 +03:00
This commit is contained in:
romanb 2007-11-19 10:00:44 +00:00
parent d5cc06e7b8
commit ba9e4676fa
4 changed files with 50 additions and 39 deletions

View File

@ -305,6 +305,9 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
return true; return true;
} }
/**
* @todo Description. See also the todo for deleteMultiple().
*/
public function deleteRecord(Doctrine_Record $record) public function deleteRecord(Doctrine_Record $record)
{ {
$ids = $record->identifier(); $ids = $record->identifier();
@ -329,46 +332,54 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
* deletes all records from the pending delete list * deletes all records from the pending delete list
* *
* @return void * @return void
* @todo Refactor. Maybe move to the Connection class? Sometimes UnitOfWork constructs
* queries itself and sometimes it leaves the sql construction to Connection.
* This should be changed.
*/ */
public function deleteMultiple(array $records) public function deleteMultiple(array $records)
{ {
foreach ($this->delete as $name => $deletes) { foreach ($this->delete as $name => $deletes) {
$record = false; $record = false;
$ids = array(); $ids = array();
if (is_array($deletes[count($deletes)-1]->getTable()->getIdentifier())) { // Note: Why is the last element's table identifier checked here and then
if (count($deletes) > 0) { // the table object from $deletes[0] used???
$query = 'DELETE FROM ' if (is_array($deletes[count($deletes)-1]->getTable()->getIdentifier()) &&
. $this->conn->quoteIdentifier($deletes[0]->getTable()->getTableName()) count($deletes) > 0) {
. ' WHERE '; $table = $deletes[0]->getTable();
$query = 'DELETE FROM '
. $this->conn->quoteIdentifier($table->getTableName())
. ' WHERE ';
$params = array(); $params = array();
$cond = array(); $cond = array();
foreach ($deletes as $k => $record) { foreach ($deletes as $k => $record) {
$ids = $record->identifier(); $ids = $record->identifier();
$tmp = array(); $tmp = array();
foreach (array_keys($ids) as $id) { foreach (array_keys($ids) as $id) {
$tmp[] = $id . ' = ? '; $tmp[] = $table->getColumnName($id) . ' = ? ';
}
$params = array_merge($params, array_values($ids));
$cond[] = '(' . implode(' AND ', $tmp) . ')';
} }
$query .= implode(' OR ', $cond); $params = array_merge($params, array_values($ids));
$cond[] = '(' . implode(' AND ', $tmp) . ')';
$this->conn->execute($query, $params);
} }
$query .= implode(' OR ', $cond);
$this->conn->execute($query, $params);
} else { } else {
foreach ($deletes as $k => $record) { foreach ($deletes as $k => $record) {
$ids[] = $record->getIncremented(); $ids[] = $record->getIncremented();
} }
// looks pretty messy. $record should be already out of scope. ugly php behaviour.
// even the php manual agrees on that and recommends to unset() the last element
// immediately after the loop ends.
$table = $record->getTable();
if ($record instanceof Doctrine_Record) { if ($record instanceof Doctrine_Record) {
$params = substr(str_repeat('?, ', count($ids)), 0, -2); $params = substr(str_repeat('?, ', count($ids)), 0, -2);
$query = 'DELETE FROM ' $query = 'DELETE FROM '
. $this->conn->quoteIdentifier($record->getTable()->getTableName()) . $this->conn->quoteIdentifier($record->getTable()->getTableName())
. ' WHERE ' . ' WHERE '
. $record->getTable()->getIdentifier() . $table->getColumnName($table->getIdentifier())
. ' IN(' . $params . ')'; . ' IN(' . $params . ')';
$this->conn->execute($query, $ids); $this->conn->execute($query, $ids);
@ -395,15 +406,14 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
if ($rel instanceof Doctrine_Relation_ForeignKey) { if ($rel instanceof Doctrine_Relation_ForeignKey) {
$saveLater[$k] = $rel; $saveLater[$k] = $rel;
} elseif ($rel instanceof Doctrine_Relation_LocalKey) { } else if ($rel instanceof Doctrine_Relation_LocalKey) {
// ONE-TO-ONE relationship // ONE-TO-ONE relationship
$obj = $record->get($rel->getAlias()); $obj = $record->get($rel->getAlias());
// Protection against infinite function recursion before attempting to save // Protection against infinite function recursion before attempting to save
if ($obj instanceof Doctrine_Record && if ($obj instanceof Doctrine_Record && $obj->isModified()) {
$obj->isModified()) {
$obj->save($this->conn); $obj->save($this->conn);
/** /** Can this be removed?
$id = array_values($obj->identifier()); $id = array_values($obj->identifier());
foreach ((array) $rel->getLocal() as $k => $field) { foreach ((array) $rel->getLocal() as $k => $field) {
@ -472,11 +482,9 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
foreach ($record->getTable()->getRelations() as $fk) { foreach ($record->getTable()->getRelations() as $fk) {
if ($fk->isComposite()) { if ($fk->isComposite()) {
$obj = $record->get($fk->getAlias()); $obj = $record->get($fk->getAlias());
if ( $obj instanceof Doctrine_Record && if ($obj instanceof Doctrine_Record &&
$obj->state() != Doctrine_Record::STATE_LOCKED) { $obj->state() != Doctrine_Record::STATE_LOCKED) {
$obj->delete($this->conn); $obj->delete($this->conn);
} }
} }
} }
@ -568,6 +576,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
return true; return true;
} }
/** /**
* inserts a record into database * inserts a record into database
* *

View File

@ -53,6 +53,7 @@ class Doctrine_Hydrate_Record extends Doctrine_Locator_Injectable
return $coll->key(); return $coll->key();
} }
public function initRelated($record, $name) public function initRelated($record, $name)
{ {
if ( ! is_array($record)) { if ( ! is_array($record)) {
@ -62,6 +63,7 @@ class Doctrine_Hydrate_Record extends Doctrine_Locator_Injectable
} }
return false; return false;
} }
public function registerCollection(Doctrine_Collection $coll) public function registerCollection(Doctrine_Collection $coll)
{ {
$this->_collections[] = $coll; $this->_collections[] = $coll;
@ -93,10 +95,12 @@ class Doctrine_Hydrate_Record extends Doctrine_Locator_Injectable
} }
return true; return true;
} }
public function getNullPointer() public function getNullPointer()
{ {
return self::$_null; return self::$_null;
} }
public function getElement(array $data, $component) public function getElement(array $data, $component)
{ {
if ( ! isset($this->_tables[$component])) { if ( ! isset($this->_tables[$component])) {
@ -104,15 +108,9 @@ class Doctrine_Hydrate_Record extends Doctrine_Locator_Injectable
$this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false); $this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
} }
//echo "..before..";
//Doctrine::dump($data);
$this->_tables[$component]->setData($data); $this->_tables[$component]->setData($data);
$record = $this->_tables[$component]->getRecord(); $record = $this->_tables[$component]->getRecord();
//echo "..after..";
//Doctrine::dump($record->getData());
if ( ! isset($this->_records[$record->getOid()]) ) { if ( ! isset($this->_records[$record->getOid()]) ) {
$record->clearRelated(); $record->clearRelated();
$this->_records[$record->getOid()] = $record; $this->_records[$record->getOid()] = $record;
@ -120,6 +118,7 @@ class Doctrine_Hydrate_Record extends Doctrine_Locator_Injectable
return $record; return $record;
} }
public function flush() public function flush()
{ {
// take snapshots from all initialized collections // take snapshots from all initialized collections

View File

@ -106,6 +106,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
/** /**
* @var array $_modified an array containing field names that have been modified * @var array $_modified an array containing field names that have been modified
* @todo Better name? $_modifiedFields?
*/ */
protected $_modified = array(); protected $_modified = array();
@ -761,7 +762,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
/** /**
* load * load
* loads all the unitialized properties from the database * loads all the uninitialized properties from the database
* *
* @return boolean * @return boolean
*/ */
@ -790,7 +791,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
$value = self::$_null; $value = self::$_null;
if (isset($this->_data[$fieldName])) { if (isset($this->_data[$fieldName])) {
// check if the property is null (= it is the Doctrine_Null object located in self::$_null) // check if the value is the Doctrine_Null object located in self::$_null)
if ($this->_data[$fieldName] === self::$_null && $load) { if ($this->_data[$fieldName] === self::$_null && $load) {
$this->load(); $this->load();
} }
@ -1379,6 +1380,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
* returns the value of autoincremented primary key of this object (if any) * returns the value of autoincremented primary key of this object (if any)
* *
* @return integer * @return integer
* @todo Better name?
*/ */
final public function getIncremented() final public function getIncremented()
{ {

View File

@ -1215,6 +1215,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
* *
* @param Doctrine_Record $record record to be added * @param Doctrine_Record $record record to be added
* @return boolean * @return boolean
* @todo Better name? registerRecord?
*/ */
public function addRecord(Doctrine_Record $record) public function addRecord(Doctrine_Record $record)
{ {