Skip to content

Commit

Permalink
Merge pull request #246 from haneev/master
Browse files Browse the repository at this point in the history
Bug using a one-relation, MongoDBRef foreign key and the reference does not exists
  • Loading branch information
Sammaye committed Feb 25, 2015
2 parents 6ea3575 + 3e27ec9 commit 082cbbc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion EMongoDocument.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,10 @@ public function getAttributeLabel($attribute)
*/
public function populateRecord($attributes, $callAfterFind = true, $partial = false)
{
if($attributes === false){
if($attributes === false || $attributes === null){
return null;
}

$record = $this->instantiate($attributes);
$record->setScenario('update');
$record->setIsNewRecord(false);
Expand Down
13 changes: 9 additions & 4 deletions EMongoModel.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function unsetAttributes($names = null)
*/
public function populateRecord($attributes, $runEvent = true)
{
if($attributes === false){
if($attributes === false || $attributes === null){
return null;
}

Expand Down Expand Up @@ -404,10 +404,15 @@ public function getRelated($name, $refresh = false, $params = array())
$result = array();
foreach($pk as $singleReference){
$row = $this->populateReference($singleReference, $cname);
if($row){
array_push($result, $row);
}

// When $row does not exists it will return null. It will not add it to $result
array_push($result, $row);
}

// When $row is null count($result) will be 0 and $result will be an empty array
// Because we are a one relation we want to return null when a row does not exists
// Currently it was returning an empty array

if($relation[0] === 'one' && count($result) > 0){
$result = $result[0];
}
Expand Down

0 comments on commit 082cbbc

Please sign in to comment.