Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bakaphp/http
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken committed Nov 26, 2018
2 parents 6fb0856 + a0b7d04 commit c85bd17
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ Thats it, your controller now manages the custom fields as if they wher properti

# Normal API CRUD

Just extend your API controller from CrudController and you will have the following functions
Just extend your API controller from CrudController and you will have the following functions:

The CRUD handles the default behaviero:
- GET /v1/leads -> get all
Expand Down
31 changes: 19 additions & 12 deletions src/QueryParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Baka\Http;

use Exception;
use Phalcon\Mvc\Model;
use Phalcon\Di;
use Phalcon\Mvc\Model\ResultsetInterface;
use Phalcon\Mvc\Model;

/**
* Base QueryParser. Parse GET request for a API to a array Phalcon Model find and FindFirst can intepret
Expand Down Expand Up @@ -145,7 +145,7 @@ public function parseSearchParameters(string $unparsed): array
protected function parseSubquery(string $unparsed): array
{
// Strip parens that come with the request string
$tableName = explode('(', $unparsed, 2);
$tableName = explode("(", $unparsed, 2);
//print_r($tableName);die();
$tableName = strtolower($tableName[0]);

Expand All @@ -168,7 +168,7 @@ protected function parseSubquery(string $unparsed): array
$action = 'in';
$fieldsToRelate = explode('::', $splitFields[0]);
} else {
throw new \Exception('Error Processing Subquery', 1);
throw new \Exception("Error Processing Subquery", 1);
}

$subquery = [
Expand All @@ -190,7 +190,7 @@ protected function parseSubquery(string $unparsed): array
protected function prepareSearch(array $unparsed, bool $isSearch = false, $hasSubquery = false): array
{
$statement = [
'conditions' => '1 = 1',
'conditions' => "1 = 1",
'bind' => [],
];

Expand All @@ -210,8 +210,15 @@ protected function prepareSearch(array $unparsed, bool $isSearch = false, $hasSu
$keys = array_keys($tmpMapped);
$values = array_values($tmpMapped);

$di = Di::getDefault();

foreach ($keys as $key => $field) {
$conditions .= " AND {$field} LIKE ?{$key}";
if ($di->get('config')->database->adapter == 'Postgresql') {
$conditions .= " AND CAST({$field} AS TEXT) LIKE ?{$key}";
}
else {
$conditions .= " AND {$field} LIKE ?{$key}";
}
}

if (isset($betweenMap)) {
Expand Down Expand Up @@ -259,31 +266,31 @@ protected function parsePartialFields(string $unparsed): array
}

/**
* Based on the given relaitonship, add the relation array to the Resultset
* Based on the given relaitonship , add the relation array to the Resultset
*
* @param string $relationships
* @param [array|object] $results
* @return array
* @param [array|object] $results by reference to clean the object
* @return mixed
*/
public static function parseRelationShips(string $relationships, &$results): array
{
$relationships = explode(',', $relationships);

$newResults = [];

// if its a list
//if its a list
if ($results instanceof ResultsetInterface && count($results) >= 1) {
foreach ($results as $key => $result) {
//clean records conver to array
$newResults[$key] = $result->toArray();
$newResults[$key] = $result->toArray();
foreach ($relationships as $relationship) {
if ($results[$key]->$relationship) {
$newResults[$key][$relationship] = $results[$key]->$relationship;
}
}
}
} else {
// if its only 1 record
//if its only 1 record
if ($results instanceof Model) {
$newResults = $results->toArray();
foreach ($relationships as $relationship) {
Expand Down
2 changes: 1 addition & 1 deletion src/QueryParserCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ public function parseSearchParameters(string $unparsed): array
*/
private function getTextFields($table): array
{
$columnsData = $this->db->describeColumns($table);
$columnsData = $this->model->getReadConnection()->describeColumns($table);
$textFields = [];

foreach ($columnsData as $column) {
Expand Down
11 changes: 11 additions & 0 deletions src/RouterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,15 @@ public function options(string $pattern, array $param) : void
{
$this->call('options', $pattern, $param[0], $param[1], $param);
}

/**
* Instead of using magic we define each method function
*
* @param string $pattern
* @param array $param
*/
public function head(string $pattern, array $param)
{
return $this->call('head', $pattern, $param[0], $param[1]);
}
}

0 comments on commit c85bd17

Please sign in to comment.