diff --git a/.travis.yml b/.travis.yml index 4b5923f..e11281f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/src/Slick/Orm/Entity.php b/src/Slick/Orm/Entity.php index 69c6442..af64778 100644 --- a/src/Slick/Orm/Entity.php +++ b/src/Slick/Orm/Entity.php @@ -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; @@ -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; @@ -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()); + } } diff --git a/src/Slick/Orm/Relation/AbstractRelation.php b/src/Slick/Orm/Relation/AbstractRelation.php index b327c3a..40c83c5 100644 --- a/src/Slick/Orm/Relation/AbstractRelation.php +++ b/src/Slick/Orm/Relation/AbstractRelation.php @@ -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; @@ -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; } /** @@ -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; } } diff --git a/src/Slick/Orm/Relation/AbstractSingleRelation.php b/src/Slick/Orm/Relation/AbstractSingleRelation.php index ca8ee9a..9e8ac38 100644 --- a/src/Slick/Orm/Relation/AbstractSingleRelation.php +++ b/src/Slick/Orm/Relation/AbstractSingleRelation.php @@ -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); + } } }