diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index 879e8bd8f..b5f0c6341 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -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 diff --git a/tests/RecordTestCase.php b/tests/RecordTestCase.php index 6b45aaf78..d8ea937b1 100644 --- a/tests/RecordTestCase.php +++ b/tests/RecordTestCase.php @@ -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); + } + } ?>