From 5602e86c9e2198f1f2f7db995977ad7eaf2e2d72 Mon Sep 17 00:00:00 2001 From: oscarotero Date: Thu, 24 Sep 2015 12:49:21 +0200 Subject: [PATCH] fixed Json field --- src/BaseRow.php | 2 +- src/Entity.php | 2 +- src/Fields/Json.php | 6 +++++- src/Queries/LimitTrait.php | 2 +- src/Queries/Sqlite/CompiledOptionsTrait.php | 2 +- src/Queries/WhereExtendedTrait.php | 2 +- src/Queries/WhereTrait.php | 2 +- src/Row.php | 9 +++++---- tests/RowTest.php | 8 ++++---- 9 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/BaseRow.php b/src/BaseRow.php index 04110d8..c087f96 100644 --- a/src/BaseRow.php +++ b/src/BaseRow.php @@ -3,7 +3,7 @@ /** * Base class used by Row and RowCollection - * + * * @property mixed $id */ abstract class BaseRow implements RowInterface diff --git a/src/Entity.php b/src/Entity.php index b4508ff..ec72a21 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -63,7 +63,7 @@ protected function init() /** * Register a new field type - * + * * @param string $name * @param string|null $type */ diff --git a/src/Fields/Json.php b/src/Fields/Json.php index d1fdb75..85e368a 100644 --- a/src/Fields/Json.php +++ b/src/Fields/Json.php @@ -11,7 +11,11 @@ class Json extends Field */ public function dataToDatabase($data) { - return json_encode($data); + if (!is_string($data)) { + return json_encode($data); + } + + return $data; } /** diff --git a/src/Queries/LimitTrait.php b/src/Queries/LimitTrait.php index 5ec90e7..d7ded4a 100644 --- a/src/Queries/LimitTrait.php +++ b/src/Queries/LimitTrait.php @@ -3,7 +3,7 @@ /** * Common function to manage LIMIT clause - * + * * @property \SimpleCrud\Entity $entity */ trait LimitTrait diff --git a/src/Queries/Sqlite/CompiledOptionsTrait.php b/src/Queries/Sqlite/CompiledOptionsTrait.php index c219721..37b4d3f 100644 --- a/src/Queries/Sqlite/CompiledOptionsTrait.php +++ b/src/Queries/Sqlite/CompiledOptionsTrait.php @@ -6,7 +6,7 @@ /** * Manages a database select count query in Mysql databases - * + * * @property \SimpleCrud\Entity $entity */ trait CompiledOptionsTrait diff --git a/src/Queries/WhereExtendedTrait.php b/src/Queries/WhereExtendedTrait.php index 66b3559..dccc205 100644 --- a/src/Queries/WhereExtendedTrait.php +++ b/src/Queries/WhereExtendedTrait.php @@ -5,7 +5,7 @@ /** * Common function to manage WHERE clause - * + * * @property \SimpleCrud\Entity $entity */ trait WhereExtendedTrait diff --git a/src/Queries/WhereTrait.php b/src/Queries/WhereTrait.php index 4b9e9e0..bee190a 100644 --- a/src/Queries/WhereTrait.php +++ b/src/Queries/WhereTrait.php @@ -3,7 +3,7 @@ /** * Common function to manage WHERE clause - * + * * @property \SimpleCrud\Entity $entity */ trait WhereTrait diff --git a/src/Row.php b/src/Row.php index 54742ea..a68c9c4 100644 --- a/src/Row.php +++ b/src/Row.php @@ -41,7 +41,7 @@ public function __get($name) switch ($this->entity->getRelation($name)) { case Entity::RELATION_HAS_ONE: return $this->values[$name] = $this->select($name)->one(); - + case Entity::RELATION_HAS_MANY: case Entity::RELATION_HAS_BRIDGE: return $this->values[$name] = $this->select($name)->all(); @@ -205,7 +205,8 @@ public function save($duplications = false, $externalRelations = false) * * @return $this */ - protected function saveExternalRelations() { + protected function saveExternalRelations() + { $extData = array_diff_key($this->values, $this->entity->fields); $db = $this->entity->getDb(); @@ -225,7 +226,7 @@ protected function saveExternalRelations() { if ($this->entity->hasMany($entity)) { $entity->update() ->data([ - $this->entity->foreignKey => $this->id + $this->entity->foreignKey => $this->id, ]) ->by('id', $ids) ->run(); @@ -244,7 +245,7 @@ protected function saveExternalRelations() { $bridge->insert() ->data([ $this->entity->foreignKey => $this->id, - $entity->foreignKey => $id + $entity->foreignKey => $id, ]) ->run(); } diff --git a/tests/RowTest.php b/tests/RowTest.php index 03f37b1..031114c 100644 --- a/tests/RowTest.php +++ b/tests/RowTest.php @@ -47,7 +47,7 @@ public function testPost() public function testCustomFunction() { $post = $this->db->post->create([ - 'title' => 'THIS IS THE TITLE' + 'title' => 'THIS IS THE TITLE', ])->save(); $this->assertSame('this is the title', $post->getTitleLowerCase()); @@ -58,7 +58,7 @@ public function testCustomFunction() } } -class DefaultEntity extends Entity +class DefaultEntity extends Entity { protected function init() { @@ -70,8 +70,8 @@ protected function init() return $row->getTitleLowerCase(); }); - $this->collection->registerMethod('sumIds', function($collection) { + $this->collection->registerMethod('sumIds', function ($collection) { return array_sum($collection->id); }); } -} \ No newline at end of file +}