Skip to content

Commit

Permalink
FIX No longer caching has_one after ID change
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensby committed Jun 16, 2015
1 parent 27f272d commit 6169bf2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions model/DataObject.php
Expand Up @@ -2316,6 +2316,10 @@ public function isChanged($fieldName = null, $changeLevel = 1) {
* @return DataObject $this
*/
public function setField($fieldName, $val) {
//if it's a has_one component, destroy the cache
if (substr($fieldName, -2) == 'ID') {
unset($this->components[substr($fieldName, 0, -2)]);
}
// Situation 1: Passing an DBField
if($val instanceof DBField) {
$val->Name = $fieldName;
Expand Down
9 changes: 9 additions & 0 deletions tests/model/DataObjectTest.php
Expand Up @@ -311,6 +311,7 @@ public function testHasManyRelationships() {
public function testHasOneRelationship() {
$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
$player1 = $this->objFromFixture('DataObjectTest_Player', 'player1');
$player2 = $this->objFromFixture('DataObjectTest_Player', 'player2');

// Add a captain to team 1
$team1->setField('CaptainID', $player1->ID);
Expand All @@ -325,6 +326,14 @@ public function testHasOneRelationship() {
'Player 1 is the captain');
$this->assertEquals($team1->getComponent('Captain')->FirstName, 'Player 1',
'Player 1 is the captain');

$team1->CaptainID = $player2->ID;
$team1->write();

$this->assertEquals($player2->ID, $team1->Captain()->ID);
$this->assertEquals($player2->ID, $team1->getComponent('Captain')->ID);
$this->assertEquals('Player 2', $team1->Captain()->FirstName);
$this->assertEquals('Player 2', $team1->getComponent('Captain')->FirstName);
}

/**
Expand Down

0 comments on commit 6169bf2

Please sign in to comment.