Developer friendly database library for vtiger. Hamper improove the code quality and the readbility of your PHP code around database access and manipulation.
Here is a list of compelling reasons to use it
- Avoid old-style loop over results.
- Use by default associative array for fields.
You can install the package via composer:
composer require javanile/hamper
You simply get your $hdb
object to access on database
require_once 'vendor/autoload.php';
use Javanile\Hamper\Hamper;
$hdb = Hamper::getInstance();
😎 The documentation lists all the functions you can use to make the code simple and easy to read.
The access you have on the data inside the database is based on extraction methods that return array
and that you can easily manipulate with foreach
.
Use the functions well because they allow you to take a single record or a list of records or all the values of a column.
Before starting to use it, read the list of functions carefully, and you will automatically use the best one based on the context.
For each function you are also presented, the "😿 Legacy", the version of the old style Vtiger code you can replace with Hamper functions,
comparing them, and you will realize how Hamper improves your work.
- Execute query -
$hdb->query(...)
- Get a single record -
$hdb->fetch(...)
- Get a list of records -
$hdb->fetchAll(...)
- Get a value from record -
$hdb->fetchValue(...)
- Get value by key column -
$hdb->value(...)
- Check if record exists -
$hdb->exists(...)
- Insert a record -
$hdb->insert(...)
- Get last ID -
$hdb->lastInsertId(...)
- Update a single record -
$hdb->update(...)
- Delete a single record -
$hdb->delete(...)
- Create new table -
$hdb->create(...)
Executes the given parametric query
$hdb->query($sql, $params = [], $options = [])
This method is useful to handle this situations
// Execute simple query
$hdb->query("SET NAMES utf8");
// Execute prepare query
$hdb->query("UPDATE vtiger_users SET language = ? WHERE user_name = ?", ["en_us", "admin"]);
Please, replace this kind of legacy code with the $hdb->query(...)
function
Fetches the next row from the result set rows by the given parametric query.
$hdb->fetch($sql, $params = [], $options = [])
This method is useful to handle this situations
// Execute simple query
$hdb->query("SET NAMES utf8");
// Execute prepare query
$hdb->query("UPDATE vtiger_users SET language = ? WHERE user_name = ?", ["en_us", "admin"]);
Please, replace this kind of legacy code with the $hdb->fetch(...)
function
Returns an array containing all of the result set rows by the given parametric query.
query($sql, $params = [], $options = [])
This method is useful to handle this situations
// Execute simple query
$hdb->query("SET NAMES utf8");
// Execute prepare query
$hdb->query("UPDATE vtiger_users SET language = ? WHERE user_name = ?", ["en_us", "admin"]);
Please, replace this kind of legacy code with the $hdb->fetchAll(...)
function
Fetches the next row from the result set rows by the given parametric query.
$hdb->fetchValue($sql, $params = [], $options = [])
This method is useful to handle this situations
$crmId = $hdb->fetchValue("SELECT crmid FROM vtiger_crmentity WHERE setype=? AND deleted=0", [$module]);
Please, replace this kind of legacy code with the $hdb->fetchValue(...)
function
$adb = \PearDatabase::getInstance();
$result = $adb->pquery("SELECT tabid FROM vtiger_tab WHERE name=?", [$setype]);
$tabId = $adb->query_result($result, 0, "tabid");
Execute a query to check if record with specific key and value exists.
query($sql, $params = [], $options = [])
This method is useful to handle this situations
// Execute simple query
$hdb->query("SET NAMES utf8");
// Execute prepare query
$hdb->query("UPDATE vtiger_users SET language = ? WHERE user_name = ?", ["en_us", "admin"]);
Please, replace this kind of legacy code with the $hdb->value(...)
function
Execute a query to check if record with specific key and value exists.
query($sql, $params = [], $options = [])
This method is useful to handle this situations
// Execute simple query
$hdb->query("SET NAMES utf8");
// Execute prepare query
$hdb->query("UPDATE vtiger_users SET language = ? WHERE user_name = ?", ["en_us", "admin"]);
Please, replace this kind of legacy code with the $hdb->exists(...)
function
Inserts the given record within the selected table.
query($sql, $params = [], $options = [])
This method is useful to handle this situations
// Execute simple query
$hdb->query("SET NAMES utf8");
// Execute prepare query
$hdb->query("UPDATE vtiger_users SET language = ? WHERE user_name = ?", ["en_us", "admin"]);
Please, replace this kind of legacy code with the $hdb->insert(...)
function
Return last insert ID value for the selected table.
query($sql, $params = [], $options = [])
This method is useful to handle this situations
// Execute simple query
$hdb->query("SET NAMES utf8");
// Execute prepare query
$hdb->query("UPDATE vtiger_users SET language = ? WHERE user_name = ?", ["en_us", "admin"]);
Please, replace this kind of legacy code with the $hdb->lastInsertId(...)
function
Updates the given record with the given data.
query($sql, $params = [], $options = [])
This method is useful to handle this situations
// Execute simple query
$hdb->query("SET NAMES utf8");
// Execute prepare query
$hdb->query("UPDATE vtiger_users SET language = ? WHERE user_name = ?", ["en_us", "admin"]);
Please, replace this kind of legacy code with the $hdb->update(...)
function
Deletes the given record within the given table.
query($sql, $params = [], $options = [])
This method is useful to handle this situations
// Execute simple query
$hdb->query("SET NAMES utf8");
// Execute prepare query
$hdb->query("UPDATE vtiger_users SET language = ? WHERE user_name = ?", ["en_us", "admin"]);
Please, replace this kind of legacy code with the $hdb->delete(...)
function
query($sql, $params = [], $options = [])
This method is useful to handle this situations
Please, replace this kind of legacy code with the $hdb->create(...)
function
Please see CHANGELOG for more information on what has changed recently.
$ make install
$ make tdd take=tests/HamperDatabaseTest.php
Please see CONTRIBUTING for details.
If you discover any security related issues, please email bianco@javanile.org instead of using the issue tracker.
We highly appreciate if you create a social post on Twitter with following button
This project exists thanks to all the people who contribute.
Javanile is a community project agency based in Sicily, Italy. You'll find an overview of all our projects on our website.
Does your business depend on our contributions? Reach out us on Patreon.
The MIT License (MIT). Please see License File for more information.