This component connects, registers, reads, edits and deletes records in the database.
composer require coffeebreaks/database
Connect with PDO
define('DB', [
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'name' => 'dbdebug',
'port' => 3306,
'driver' => 'mysql', //default = mysql
'options' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
]
]);
In the folder app/Models create the class Users
<?php
namespace App\Models;
use Database\Database;
class Users extends Database
{
protected $table = "users";
}
use App\Models\Users;
$user = new Users();
$user->name = "Guilherme";
$user->age = 26;
$user->save();
var_dump($user);
$user = new Users();
$user->id = 1;
$user->name = "Guilherme K.";
$user->age = 26;
$user->save();
var_dump($user);
$user = new Users();
var_dump($user->all()->getGet());
$user = new Users();
$user->findById(1);
var_dump($user->getGet());
$user = new Users();
$user->findByEmail("[email protected]");
var_dump($user->getGet());
$user = new Users();
var_dump($user->all()->orderBy('collumn ASC'));