Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.

Commit

Permalink
-Update to search function, now accepts type of query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcox17 committed Jun 27, 2018
1 parent e7a7eb0 commit 78feb6b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,24 @@ var_dump($obj);

#### Search

This function is a sql query builder wrapped around the query function.
This function is a sql query builder wrapped around the query function. Accepts instance of laravels QueryBuilder.
```php
$search = [];
$search[] = [
'firstname' => ['=', 'John'],
'lastname' => ['=', 'Smith'],
'login_date' => ['>=', '00-00-00 00:00:00'],
];
$query = DB::table('Leads')->select('id', 'firstname', 'lastname')->where('firstname', 'John');

$obj = Vtiger::search('Leads', $search);
$obj = Vtiger::search('Leads', $query);

//loop over result
foreach($obj->result as $result) {
// do something
}
```

By default the function will quote but not escape your inputs, if you wish for your data to not be quoted, set the 3rd paramater to false like so:
```php
$obj = Vtiger::search('Leads', $query, false);
```

Also keep in mind that Victiger has several limitations on it's sql query capabilities. You can not use conditional grouping i.e "where (firstname = 'John' AND 'lastname = 'Doe') OR (firstname = 'Jane' AND lastname = 'Smith') will fail.
#### Query

To use the [Query Operation](http://community.vtiger.com/help/vtigercrm/developers/third-party-app-integration.html#query-operation), you first need to create a SQL query.
Expand Down
20 changes: 8 additions & 12 deletions src/Vtiger.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,23 +320,19 @@ public function query($query)
return $this->_processResult($response);
}

public function search($module, $array)
public function search($query, $quote = true)
{
if (empty($array) || !$module) {
throw VtigerError::init($this->vTigerErrors, 4);
}

$query = DB::table($module);
foreach ($array as $item) {
$query->where(key($item), $item[key($item)][0], $item[key($item)][1]);
}

$bindings = $query->getBindings();
$queryString = $query->toSQL();

foreach ($bindings as $binding) {
$queryString = preg_replace('/\?/', DB::connection()->getPdo()->quote($binding), $queryString, 1);
if ($quote) {
$queryString = preg_replace('/\?/', DB::connection()->getPdo()->quote($binding), $queryString, 1);
} else {
$queryString = preg_replace('/\?/', $binding, $queryString, 1);
}
}
$queryString = str_replace('`', '', $queryString) . ';';

return $this->query($queryString);
}
Expand Down Expand Up @@ -582,7 +578,7 @@ protected function _processResponseError($processedData)
if (!isset($processedData->error)) {
throw VtigerError::init($this->vTigerErrors, 3);
}

dd($processedData);
throw VtigerError::init($this->vTigerErrors, 0, $processedData->error->message);
}
}

0 comments on commit 78feb6b

Please sign in to comment.