danidoble/database

ORM mysql mariadb Database

v1.0.1 2022-04-13 22:16 UTC

This package is auto-updated.

Last update: 2024-11-07 14:11:42 UTC


README

Mini ORM

installation

composer require danidoble/database

or

composer.phar require danidoble/database

Configuration

If you use a loader of credentials like vlucas/phpdotenv add inside of file .env add this credentials

  • DB_NAME="YOUR_DB_NAME"
  • DB_USER="YOUR_DB_USER"
  • DB_HOST="YOUR_DB_HOST"
  • DB_PASS="YOUR_DB_PASS"

If you don't use libraries to load credentials you can add in your code NOT RECOMMENDED

  • $_ENV['DB_NAME']="YOUR_DB_NAME";
  • $_ENV['DB_USER']="YOUR_DB_USER";
  • $_ENV['DB_HOST']="YOUR_DB_HOST";
  • $_ENV['DB_PASS']="YOUR_DB_PASS";

How to use

You can configurate name of id doing: Sql::from('users')->id('name_id')

GET

To get all data

use Danidoble\Database\Sql;

$users = Sql::from('users')->get();

var_dump($users);

To paginate data

use Danidoble\Database\Sql;

$users = Sql::from('users')->get();

var_dump($users);

Get only one

use Danidoble\Database\Sql;

$user = Sql::from('users')->first();

var_dump($user);

Get only selected by id

use Danidoble\Database\Sql;

$user = Sql::from('users')->find(1);

var_dump($user);

INSERT

use Danidoble\Database\Sql;

$user = new Sql();
$user->name = "Gregory";
$user->last_name = "Hui";
$user->save();

UPDATE

update after get

use Danidoble\Database\Sql;

$user = Sql::from('users')->id('name_id')->find(1);
$user->name = "somthing else";
$user->save();

update without get

use Danidoble\Database\Sql;

$user = Sql::from('users')
    ->set('name','Pedro')
    ->set('last_name','Crox')
    ->where('name','Gregory','=')
    ->update();

DELETE

soft delete (needed field deleted_at in database) otherwise is deleted

use Danidoble\Database\Sql;

$user = Sql::from('users')->find(1);
$user->delete();

hard delete

use Danidoble\Database\Sql;

$user = Sql::from('users')->find(1);
$user->forceDelete();