All the methods and a description about them will be listed below. All class Crud.class.php is documented.
You can install CrudAdanved manually
require_once "src/CrudAdvanced/DataBase.php";
require_once "src/CrudAdvanced/Val.php";
require_once "src/CrudAdvanced/Crud.php";
use CrudAdvanced\DataBase;
use CrudAdvanced\Val;
use CrudAdvanced\Crud;
You can install CrudAdanved with composer With command:
composer require yohancayres/crud-advanced
or adding in composer.json
{
"require": {
"yohancayres/crud-advanced": "dev-master"
}
}
- _set($key, $value) - Set or change the value of an attribute;
- _get($key) - Get the value of an attribute;
- loadData(array $data) - Loads data from an array to object attributes;
- requiredParam(array $params) - Checks whether the required attribute exists;
- getParamArray(array $params) - Get an array of object attributes;
- dbInsert() - Inserts the attributes of the object in the database;
- dbUpdateAll() - Refreshes all attributes in the database;
- dbUpdateAtts($atts) - Updates specific attributes in the database;
- dbUpdate($attr, $newvalue) - Defines and updates the value of an attribute in the database;
- dbUpdateIncrease($attr, $amount) - Updates an attribute by incrementing its value (this value can be negative);
- dbSearch($attr, $data) - Searches the database;
- dbRemove() - Removes a record from the database;
- dbCheckExists($attr) - Checks whether a record already exists;
- dbLoadData($values) - Loads all data from the database;
- dbLoadDataBy($attr, $values) - Loads all database data using an attribute defined by the first parameter;
- fetchById($id, $values) - Get database record;
- fetchRandom($values) - Gets a random record of the database;
- fetchAll($where, $loadAtts) - Get all records from the table;
- hasOne($className, $thisAttName, $classAttName, $loadAtts)- Creates a one-to-one relationship;
- hasMany($className, $thisAttName, $classAttName, $loadAtts) - Creates a one-to-many relationship;
First of all, the application needs information to connect to the database
use CrudAdvanced\DataBase;
DataBase::configure('127.0.0.1', 'root', 'password', 'dbteste');
To create a class and use the Crud Methods, you need to extends Crud to your class.
use CrudAdvanced\Val;
use CrudAdvanced\Crud;
class User extends Crud {
/* Database table definition */
public $table = "users"; // Define Table name
public $tableCols = ['name', 'email', 'password']; // Define table cols
/*
* Other class methods and attributes can be defined freely.
*/
}
This method will insert all the attributes to the database. Be aware of the name of the attributes, they must be exactly the same as the fields in the database.
$user = new User([
'name' => 'Yohan Cayres',
'email' => '[email protected]',
'password' => md5('somepassword')
]); // Create new object type User
$user->dbInsert(); // Insert at the Database
This method will load attributes from database, making the search for the defined attribute.
$user->dbLoadDataBy('email', 'id,name,password'); // Loads a database attribute by email.
echo $user->_get('id');
This method needs the id attribute, you can get it through dbLoadDataBy();
$user->_set('email', '[email protected]'); // This will set a new email
$user->dbUpdateAtts('email'); // Only the attribute 'email' will be updated
$user->dbUpdateAll(); // All the attributes will be updated
This method needs the id attribute, you can get it through dbLoadDataBy();
$user->dbUpdate('email', '[email protected]'); // Set and update directly.