diff --git a/README.md b/README.md index 87088dc..dcba64a 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,20 @@ $obj = Vtiger::retrieve($id); var_dump($obj); ``` +#### Lookup + +This function uses the Vtiger Lookup API endpoint to search for a single piece of information within multiple columns of a Vtiger module. This function is often multitudes faster than the search function. + + +```php + $dataType = 'phone'; + $phoneNumber = '1234567890'; + $module = 'Leads'; + $columns = ['phone', 'fax']; //Must be an array + + Vtiger::lookup($dataType, $phoneNumber, $module, $columns); +``` + #### Search This function is a sql query builder wrapped around the query function. Accepts instance of laravels QueryBuilder. diff --git a/src/Vtiger.php b/src/Vtiger.php index 905673d..7e7d137 100644 --- a/src/Vtiger.php +++ b/src/Vtiger.php @@ -343,15 +343,50 @@ public function search($query, $quote = true) $queryString = $queryString . ' limit ' . $matchOffset[2] . ',' . $matchLimit[2]; } - //Remove the backticks and add simicolon + //Remove the backticks and add semicolon $queryString = str_replace('`', '', $queryString) . ';'; return $this->query($queryString); } + public function lookup($dataType, $value, $module, $columns) + { + $sessionId = $this->sessionId(); + + //Update columns into the proper format + $columnsText = ''; + foreach ($columns as $column) { + $columnsText .= '"'.$column.'",'; + } + + //Trim the last comma from the string + $columnsText = substr($columnsText, 0, (strlen($columnsText) - 1)); + + //Lookup the data + try { + // send a request to retrieve a record + $response = $this->guzzleClient->request('GET', $this->url, [ + 'query' => [ + 'operation' => 'lookup', + 'sessionName' => $sessionId, + 'type' => $dataType, + 'value' => $value, + 'searchIn' => '{"'.$module.'":['.$columnsText.']}', + ], + ]); + + } catch (GuzzleException $e) { + throw VtigerError::init($this->vTigerErrors, 7, $e->getMessage()); + } + + $this->close($sessionId); + + return $this->_processResult($response); + } + /** - * Retreive a record from the VTiger API - * Format of id must be {moudler_code}x{item_id}, e.g 4x12 + * Retrieve a record from the VTiger API + * Format of id must be {module_code}x{item_id}, e.g 4x12 * * @param string $id *