Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #117 from bjornpost/fix-issue-114
Browse files Browse the repository at this point in the history
allowEmpty: implement for BelongsTo, improve for HasOne
  • Loading branch information
lox committed Apr 10, 2015
2 parents ab6c67d + 35d3a87 commit 07631a5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
34 changes: 30 additions & 4 deletions lib/Pheasant/Relationships/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@

namespace Pheasant\Relationships;

use \Pheasant\Relationship;

/**
* A BelongsTo relationship represents the weak side of a 1->1 relationship. The
* local entity has responsibility for the foreign key.
*
*/
class BelongsTo extends HasOne
class BelongsTo extends Relationship
{
private $_property;
private $_allowEmpty;

/**
* Constructor
*
*/
public function __construct($class, $local, $foreign=null, $allowEmpty=false)
{
parent::__construct($class, $local, $foreign);
$this->_allowEmpty = $allowEmpty;
}

/* (non-phpdoc)
* @see Relationship::get()
Expand All @@ -25,11 +38,24 @@ public function get($object, $key, $cache=null)
}

if (($localValue = $object->{$this->local}) === null) {
return null;
if($this->_allowEmpty) {
return null;
} else {
throw new \Pheasant\Exception("Local value is null while not allowed");
}
}

$result = $this->query("{$this->foreign}=?", $localValue)->execute();

if(!count($result)) {
if($this->_allowEmpty) {
return null;
} else {
throw new \Pheasant\Exception("Failed to find a $key (via $this->foreign)");
}
}

return $this->hydrate($this->query("{$this->foreign}=?", $localValue)
->execute()->row());
return $this->hydrate($result->row());
}

/* (non-phpdoc)
Expand Down
6 changes: 5 additions & 1 deletion lib/Pheasant/Relationships/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public function get($object, $key, $cache=null)
}

if (($localValue = $object->{$this->local}) === null) {
return null;
if($this->_allowEmpty) {
return null;
} else {
throw new \Pheasant\Exception("Local value is null while not allowed");
}
}

$result = $this
Expand Down
2 changes: 1 addition & 1 deletion tests/Pheasant/Tests/Examples/Hero.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function relationships()
{
return array(
'Powers' => Power::hasMany('id','heroid'),
'SecretIdentity' => SecretIdentity::belongsTo('identityid','id'),
'SecretIdentity' => SecretIdentity::belongsTo('identityid','id', true),
);
}

Expand Down
11 changes: 10 additions & 1 deletion tests/Pheasant/Tests/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,20 @@ public function testFilteringCollectionsReturnedByRelationships()
$this->assertCount(1, $spiderman->Powers->filter('description LIKE ?', 'Super-human%')->toArray());
}

public function testEmptyRelationships()
public function testEmptyRelationshipsWithAllowEmpty()
{
$hero = new Hero(array('alias'=>'Spider Man'));
$hero->save();

$this->assertNull($hero->SecretIdentity);
}

public function testEmptyRelationshipsWithoutAllowEmpty()
{
$power = new Power(array('description'=>'Spider Senses'));
$power->save();

$this->setExpectedException('\Pheasant\Exception');
$foo = $power->Hero;
}
}

0 comments on commit 07631a5

Please sign in to comment.