Skip to content

Commit

Permalink
fixed Json field
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Sep 24, 2015
1 parent 7cfd59a commit 5602e86
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/BaseRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/**
* Base class used by Row and RowCollection
*
*
* @property mixed $id
*/
abstract class BaseRow implements RowInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function init()

/**
* Register a new field type
*
*
* @param string $name
* @param string|null $type
*/
Expand Down
6 changes: 5 additions & 1 deletion src/Fields/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/LimitTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/**
* Common function to manage LIMIT clause
*
*
* @property \SimpleCrud\Entity $entity
*/
trait LimitTrait
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/Sqlite/CompiledOptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* Manages a database select count query in Mysql databases
*
*
* @property \SimpleCrud\Entity $entity
*/
trait CompiledOptionsTrait
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/WhereExtendedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* Common function to manage WHERE clause
*
*
* @property \SimpleCrud\Entity $entity
*/
trait WhereExtendedTrait
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/WhereTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/**
* Common function to manage WHERE clause
*
*
* @property \SimpleCrud\Entity $entity
*/
trait WhereTrait
Expand Down
9 changes: 5 additions & 4 deletions src/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand All @@ -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();
Expand All @@ -244,7 +245,7 @@ protected function saveExternalRelations() {
$bridge->insert()
->data([
$this->entity->foreignKey => $this->id,
$entity->foreignKey => $id
$entity->foreignKey => $id,
])
->run();
}
Expand Down
8 changes: 4 additions & 4 deletions tests/RowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -58,7 +58,7 @@ public function testCustomFunction()
}
}

class DefaultEntity extends Entity
class DefaultEntity extends Entity
{
protected function init()
{
Expand All @@ -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);
});
}
}
}

0 comments on commit 5602e86

Please sign in to comment.