Permalink
Browse files

Improved Entity::__isset

  • Loading branch information...
Tharos committed Jun 3, 2014
1 parent 33c2cbf commit b4a9dc7d99227d68721e4df23e3049d62c0a82dc
Showing with 41 additions and 3 deletions.
  1. +9 −2 LeanMapper/Entity.php
  2. +3 −1 LeanMapper/Result.php
  3. +29 −0 tests/LeanMapper/Entity.isset.phpt
View
@@ -161,6 +161,7 @@ public function __set($name, $value)
* Tells whether given property exists and is not null
*
* @param string $name
* @throws LeanMapperException
* @return bool
*/
public function __isset($name)
@@ -169,6 +170,11 @@ public function __isset($name)
return $this->$name !== null;
} catch (MemberAccessException $e) {
return false;
} catch (LeanMapperException $e) {
if ($this->isDetached() and $e->getCode() === Result::ERROR_MISSING_COLUMN) {
return false;
}
throw $e;
}
}
@@ -447,7 +453,7 @@ protected function get($property, array $filterArgs = array())
try {
$value = $this->row->$column;
} catch (LeanMapperException $e) {
throw new LeanMapperException("Cannot get value of property '{$property->getName()}' in entity " . get_called_class() . ' due to low-level failure: ' . $e->getMessage());
throw new LeanMapperException("Cannot get value of property '{$property->getName()}' in entity " . get_called_class() . ' due to low-level failure: ' . $e->getMessage(), $e->getCode());
}
if ($pass !== null) {
$value = $this->$pass($value);
@@ -491,7 +497,7 @@ protected function get($property, array $filterArgs = array())
try {
$value = $this->row->$column;
} catch (LeanMapperException $e) {
throw new LeanMapperException("Cannot get value of property '{$property->getName()}' in entity " . get_called_class() . ' due to low-level failure: ' . $e->getMessage());
throw new LeanMapperException("Cannot get value of property '{$property->getName()}' in entity " . get_called_class() . ' due to low-level failure: ' . $e->getMessage(), $e->getCode());
}
if ($pass !== null) {
$value = $this->$pass($value);
@@ -707,6 +713,7 @@ private function getHasOneValue(Property $property, Relationship\HasOne $relatio
return null;
} else {
$entityClass = $this->mapper->getEntityClass($targetTable, $row);
$entity = $this->entityFactory->createEntity($entityClass, $row);
$this->checkConsistency($property, $entityClass, $entity);
$entity->makeAlive($this->entityFactory);
View
@@ -35,6 +35,8 @@ class Result implements \Iterator
const PRELOADED_KEY = 'preloaded';
const ERROR_MISSING_COLUMN = 1;
/** @var bool */
private $isDetached;
@@ -210,7 +212,7 @@ public function getDataEntry($id, $key)
$key = $this->trimAlias($key);
}
if (!array_key_exists($key, $this->data[$id])) {
throw new InvalidArgumentException("Missing '$key' column in row with id $id.");
throw new InvalidArgumentException("Missing '$key' column in row with id $id.", self::ERROR_MISSING_COLUMN);
}
return $this->data[$id][$key];
}
@@ -0,0 +1,29 @@
<?php
use LeanMapper\Entity;
use LeanMapper\Exception\Exception;
use LeanMapper\Result;
use Tester\Assert;
require_once __DIR__ . '/../bootstrap.php';
/**
* @property int $id
* @property string $name
*/
class Author extends Entity
{
}
$author = new Author;
isset($author->name);
//////////
$author->makeAlive($entityFactory, $connection, $mapper);
$author->attach(1);
Assert::exception(function () use ($author) {
isset($author->name);
}, Exception::class, null, Result::ERROR_MISSING_COLUMN);

0 comments on commit b4a9dc7

Please sign in to comment.