-
Notifications
You must be signed in to change notification settings - Fork 10
Refactor ViewJson #106
base: master
Are you sure you want to change the base?
Refactor ViewJson #106
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,13 +175,10 @@ protected function getMaxOrdering() | |
*/ | ||
protected function _beforeInsert(DatabaseContextInterface $context) | ||
{ | ||
if($this->hasProperty('ordering')) | ||
{ | ||
if($this->ordering <= 0) { | ||
$this->ordering = $this->getMaxOrdering() + 1; | ||
} else { | ||
$this->reorder($this->ordering); | ||
} | ||
if($this->ordering <= 0) { | ||
$this->ordering = $this->getMaxOrdering() + 1; | ||
} else { | ||
$this->reorder($this->ordering); | ||
} | ||
} | ||
|
||
|
@@ -192,7 +189,7 @@ protected function _beforeInsert(DatabaseContextInterface $context) | |
*/ | ||
protected function _beforeUpdate(DatabaseContextInterface $context) | ||
{ | ||
if(isset($this->order) && $this->hasProperty('ordering')) { | ||
if(isset($this->order)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the property be ordering? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The refactor doesn't touch that, it only removes the additional check for 'ordering'. |
||
$this->order($this->order); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Kodekit - http://timble.net/kodekit | ||
* | ||
* @copyright Copyright (C) 2007 - 2016 Johan Janssens and Timble CVBA. (http://www.timble.net) | ||
* @license MPL v2.0 <https://www.mozilla.org/en-US/MPL/2.0> | ||
* @link https://github.com/timble/kodekit for the canonical source repository | ||
*/ | ||
|
||
namespace Kodekit\Library; | ||
|
||
/** | ||
* Sparsable Model Behavior | ||
* | ||
* By making a model sparsable, you enables the ability for clients to choose the returned properties of a model | ||
* entity with URL query parameters. This is useful for optimizing requests, making API calls more efficient | ||
* and fast. | ||
* | ||
* A client can request to get only specific fields in the response by including a fields[TYPE] parameter. The | ||
* value of the fields parameter MUST be a comma-separated (U+002C COMMA, “,”) list that refers to the name(s) | ||
* of the fields to be returned. | ||
* | ||
* The behavior will ALAWYS include the identity key property of the specific type in the returned properties. | ||
* | ||
* Based on the Sparse Fieldsets specification in the JSON API | ||
* @link http://jsonapi.org/format/#fetching-sparse-fieldsets | ||
* | ||
* @author Johan Janssens <https://github.com/johanjanssens> | ||
* @package Kodekit\Library\Model\Behavior | ||
*/ | ||
class ModelBehaviorSparsable extends ModelBehaviorAbstract | ||
{ | ||
/** | ||
* Insert the model states | ||
* | ||
* @param ObjectMixable $mixer | ||
*/ | ||
public function onMixin(ObjectMixable $mixer) | ||
{ | ||
parent::onMixin($mixer); | ||
|
||
$mixer->getState() | ||
->insert('fields', 'cmd', array()); | ||
} | ||
|
||
/** | ||
* Parse the fields state | ||
* | ||
* @param ModelContextInterface $context A model context object | ||
* @return void | ||
*/ | ||
protected function _afterReset(ModelContextInterface $context) | ||
{ | ||
if($context->modified->contains('fields')) | ||
{ | ||
$fields = $context->state->fields; | ||
|
||
foreach ($fields as $type => $value) | ||
{ | ||
if(is_string($value)) { | ||
$fields[$type] = array_unique(explode(',', $value)); | ||
} | ||
} | ||
|
||
$context->state->fields = $fields; | ||
} | ||
} | ||
|
||
/** | ||
* Add query colums based on fields | ||
* | ||
* @param ModelContextInterface $context A model context object | ||
* @return void | ||
*/ | ||
protected function _beforeFetch(ModelContextInterface $context) | ||
{ | ||
$model = $context->getSubject(); | ||
|
||
$result = array(); | ||
$columns = $this->getTable()->getColumns(true); | ||
|
||
if ($model instanceof ModelDatabase) | ||
{ | ||
$fields = $context->state->fields; | ||
$type = $model->getIdentifier()->name; | ||
|
||
if(isset($fields[$type])) | ||
{ | ||
$result = array(); | ||
$columns = array_keys($this->getTable()->getColumns()); | ||
|
||
foreach($fields[$type] as $field) | ||
{ | ||
if(in_array($field, $columns)) | ||
{ | ||
$column = $this->getTable()->mapColumns($field); | ||
$result[] = 'tbl.'.$column; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth checking the colum exists as a table colum, OR exists as an alias column in the query that's already been constructed. That way, joined columns can also work, currently this would cause an error as tbl.foo may not exist, but foo, may, as a joined/aliased colum. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking the query itself is out of scope. Not trying to solve problems that are not there. If a situation occurs where checking the query should be needed you can extend the behavior in your own component. |
||
} | ||
} | ||
|
||
if(!empty($result)) | ||
{ | ||
$context->query->columns = array(); | ||
|
||
//Always include the identity column | ||
$result[] = 'tbl.'.$this->getTable()->getIdentityColumn(); | ||
$context->query->columns($result); | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this apply if the table has no uuid col?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No the isSupported() method explicitly check if the table has a uuid column. No need to double check it.