celtak / dog-database
A class that allows to use a MySQL database and make calls to this database not through queries but through methods
v1.0.1
2019-03-29 18:57 UTC
Requires
- php: ^7.1.0
- ext-pdo: *
- symfony/var-dumper: ^4.2
- symfony/yaml: ^4.2
This package is auto-updated.
Last update: 2025-07-08 02:47:27 UTC
README
Composer install
composer require celtak/dog-database
Documentation
Prerequisites
require __DIR__ . DIRECTORY_SEPARATOR . 'vendor/autoload.php'; use Celtak\DogDatabase;
Connect to the database
How to connect to the database?
$database = new DogDatabase([ 'host' => 'localhost:8889', 'dbname' => 'azurWeb', 'username' => 'root', 'password' => 'root' ], true);
The second parameter is a boolean that allows to display or not the errors.
A table ("users") in a database
To understand how it works, we will use a table.
Table: users
first | name | old |
---|---|---|
Mélanie | Rodrigues | 24 |
Anthony | Jones | 40 |
Audrey | Jones | 24 |
Work on a table
$database->setTable('users');
Create
How to insert new data?
// Insert new data $database->insert([ ['first', 'Henrique'], ['name', 'Rodrigues'], ['old', 33], ]);
What is the result?
id | first | name | old |
---|---|---|---|
1 | Melanie | Rodrigues | 24 |
2 | Anthony | Jones | 40 |
3 | Audrey | Jones | 24 |
4 | Henrique | Rodrigues | 33 |
Read
getAll()
Read the whole database.
$read = $database->getAll(); // All the contents of the database dump($read);
Use filters (where, order by and limit).
Example 1
$read = $database->getAll([ 'where' => ['name', 'Rodrigues'], 'orderBy' => 'old', ]); // Just "Melanie Rodrigues" and "Henrique Rodrigues" dump($read);
Example 2
$read = $database->getAll([ 'limit' => [1, 3] ]); // Just "Melanie Rodrigues", "Anthony Jones" and "Audrey Jones" dump($read);
getFiels()
Get some fields.
$read = $database->getFields(['name', 'first']); // We just get the "name" and "first" fields of only "Anthony Jones" and "Audrey Jones" dump($read);
With filters.
$read = $database->getFields(['name', 'first'], [ 'where' => ['name', 'Jones'] ]); // We just get the "name" and "first" fields from all the database dump($read);
getId()
Get by id.
$read = $database->getId(3); // We get the data related to id 3, "Audrey Jones" dump($read);
Update
update()
Using the required "where" filter.
// Modify the first name Henrique by Rik $database->update( [ ['first', 'Rik'] ], [ 'where' => ['first', 'Henrique'], ] );
updateId()
Using the id.
// Modify the first name by John and the name by Taylor from id 3 $database->updateId(3, [ ['first', 'John'], ['name', 'Taylor'], ]);
Delete
delete()
Using the required "where" filter.
// Delete all data from id 2 $database->delete([ 'where' => ['id', '2'] ]);
deleteId()
// Delete all data from id 1 $database->deleteId(1);
Custom query
To read.
dump($database->getCustomizedQuery('SELECT * FROM users WHERE id = "10"'));
To write.
$database->customizedExec('INSERT INTO utilisateurs (name, first, old) VALUES ("Henrique", "Rodrigues", "33")');
License
MIT.