added the method refreshRelated() to Doctrine_Record
This commit is contained in:
parent
f92cd7771b
commit
2f85c85cf7
@ -669,6 +669,26 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* refresh
|
||||
* refres data of related objects from the database
|
||||
*
|
||||
* @param string $name name of a related component.
|
||||
* if set, this method only refreshes the specified related component
|
||||
*/
|
||||
public function refreshRelated($name = null)
|
||||
{
|
||||
if (is_null($name)) {
|
||||
foreach ($this->_table->getRelations() as $rel) {
|
||||
$this->_references[$rel->getAlias()] = $rel->fetchRelatedFor($this);
|
||||
}
|
||||
} else {
|
||||
$rel = $this->_table->getRelation($name);
|
||||
$this->_references[$name] = $rel->fetchRelatedFor($this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getTable
|
||||
* returns the table object for this record
|
||||
|
@ -39,6 +39,7 @@ class Doctrine_Record_TestCase extends Doctrine_UnitTestCase
|
||||
$this->tables[] = 'fieldNameTest';
|
||||
$this->tables[] = 'GzipTest';
|
||||
$this->tables[] = 'Book';
|
||||
$this->tables[] = 'EntityAddress';
|
||||
parent::prepareTables();
|
||||
}
|
||||
|
||||
@ -869,5 +870,21 @@ class Doctrine_Record_TestCase extends Doctrine_UnitTestCase
|
||||
$user = $this->connection->getTable("User")->find(4);
|
||||
$this->assertTrue($user->getIterator() instanceof ArrayIterator);
|
||||
}
|
||||
|
||||
public function testRefreshRelated()
|
||||
{
|
||||
$user = $this->connection->getTable("User")->find(4);
|
||||
$user->Address[0]->address = "Address #1";
|
||||
$user->Address[1]->address = "Address #2";
|
||||
$user->save();
|
||||
$this->assertEqual(count($user->Address), 2);
|
||||
Doctrine_Query::create()->delete()->from('EntityAddress')->where('user_id = ? AND address_id = ?', array($user->id, $user->Address[1]->id))->execute();
|
||||
$user->refreshRelated('Address');
|
||||
$this->assertEqual(count($user->Address), 1);
|
||||
Doctrine_Query::create()->delete()->from('EntityAddress')->where('user_id = ? AND address_id = ?', array($user->id, $user->Address[0]->id))->execute();
|
||||
$user->refreshRelated();
|
||||
$this->assertEqual(count($user->Address), 0);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
|
Loading…
Reference in New Issue
Block a user