Skip to content

Commit

Permalink
Fixing bugs on join results from relations.
Browse files Browse the repository at this point in the history
Fixing serialization/unserialization of entities.
Add exception for undefined entity classes on relation tag declarations.
  • Loading branch information
silvamfilipe committed Oct 12, 2014
1 parent 767224d commit cb314ee
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 12 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ env:
services:
- memcached

install:
- sudo apt-get install -y --force-yes apache2 libapache2-mod-php5 php5-curl php5-intl

before_script:
- phpenv config-add custom.ini
- composer self-update
Expand Down
69 changes: 69 additions & 0 deletions src/Slick/Orm/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Slick\Orm;

use Slick\Database\RecordList;
use Slick\Database\Sql;
use Slick\Orm\Events\Delete;
use Slick\Orm\Events\Save;
Expand Down Expand Up @@ -113,6 +114,9 @@ public static function get($id)
public static function find($fields = '*')
{
$entity = new static();
if ($fields == '*') {
$fields = $entity->getTableName() .'.*';
}
Entity\Manager::getInstance()->get($entity)->refreshRelations();
$select = new \Slick\Orm\Sql\Select($entity, $fields);
return $select;
Expand Down Expand Up @@ -259,4 +263,69 @@ protected function _setData(array $data)

return $data;
}

/**
* Recursively returns this entity as an array. Used in json and
* serialization processes.
*
* @return array
*/
public function asArray()
{
$data = [];
$columns = Manager::getInstance()->get($this)->getColumns();
foreach(array_keys($columns) as $field) {
$data[trim($field, '_')] = $this->$field;
}
$relations = Manager::getInstance()->get($this)->getRelations();
foreach(array_keys($relations) as $field) {
if ($this->$field instanceof Entity) {
$data[trim($field, '_')] = $this->$field->asArray();
} elseif ($this->$field instanceof RecordList) {
$values = [];
foreach ($this->$field as $entity) {
$values[] = $entity->asArray();
}
$data[trim($field, '_')] = $values;
} else {
$data[trim($field, '_')] = $this->$field;
}
}
return $data;

}

/**
* Serializes this entity
*
* @return string
*/
public function serialize()
{
$data = $this->asArray();
$serialize = serialize($data);
return $serialize;
}

/**
* Unserialize callback handle
*
* @param string $data
* @return self
*/
public function unserialize($data)
{
$this->_createObject(unserialize($data));
return $this;
}

/**
* Returns this entity in JSON format
*
* @return string
*/
public function asJason()
{
return json_encode($this->asArray());
}
}
19 changes: 18 additions & 1 deletion src/Slick/Orm/Relation/AbstractRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Slick\Di\Container;
use Slick\Di\Definition;
use Slick\Di\ContainerBuilder;
use Slick\Orm\Exception\InvalidArgumentException;
use Slick\Orm\RelationInterface;
use Slick\Common\Inspector\Annotation;

Expand Down Expand Up @@ -119,7 +120,13 @@ public function getRelatedEntity()
*/
public function setRelatedEntity($entity)
{
if (!class_exists($entity)) {
throw new InvalidArgumentException(
"The entity class '{$entity}' does not exists."
);
}
$this->_relatedEntity = $entity;
return $this;
}

/**
Expand Down Expand Up @@ -234,10 +241,20 @@ public static function create(
$parameters = $annotation->getParameters();
unset ($parameters['_raw']);

$related = $annotation->getValue();
if (!class_exists($related)) {
$parentClass = $entity->getClassName();
throw new InvalidArgumentException(
"Unknown class defined in {$parentClass}::_{$property}. ".
"Class {$related} does not exists."
);
}

/** @var BelongsTo $relation */
$relation = new static($parameters);
$relation->setEntity($entity)->setPropertyName($property);
$relation->setRelatedEntity($annotation->getValue());

$relation->setRelatedEntity($related);
return $relation;
}
}
18 changes: 10 additions & 8 deletions src/Slick/Orm/Relation/AbstractSingleRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,29 @@ public function afterSelect(Select $event)
$relatedTable = $related->getEntity()->getTableName();
$class = $related->getEntity()->getClassName();
foreach ($data as $key => $row) {

$pmk = $this->getEntity()->getPrimaryKey();
if (isset($row[$pmk]) && is_array($row[$pmk])) {
$data[$key][$pmk] = reset($row[$pmk]);
}
$options = [];
foreach ($row as $column => $value) {
if (preg_match('/'.$relatedTable.'_(.*)/i', $column)) {
unset($data[$key][$column]);
$name = str_replace($relatedTable.'_', '', $column);
$options[$name] = $value;
if (is_array($row)) {
foreach ($row as $column => $value) {
if (preg_match('/' . $relatedTable . '_(.*)/i', $column)) {
unset($data[$key][$column]);
$name = str_replace($relatedTable . '_', '', $column);
$options[$name] = $value;
}
}
}

$data[$key][$this->getPropertyName()] = new $class($options);
}

if ($multiple) {
$event->data = $data;
} else {

}$event->data = reset($data);
$event->data = reset($data);
}
}

}

0 comments on commit cb314ee

Please sign in to comment.