1. Go to this page and download the library: Download paulinhosupriano/orm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class User extends Orm
{
/**
* User constructor.
*/
public function __construct()
{
//string "TABLE_NAME", array ["REQUIRED_FIELD_1", "REQUIRED_FIELD_2"], string "PRIMARY_KEY", bool "TIMESTAMPS"
parent::__construct("tabela", ["campo_um_obrigatorio", "campo_dois_obrigatorio"]);
}
}
use Example\Models\User;
$model = new User();
//find all users
$users = $model->find()->fetch(true);
//find all users limit 2
$users = $model->find()->limit(2)->fetch(true);
//find all users limit 2 offset 2
$users = $model->find()->limit(2)->offset(2)->fetch(true);
//find all users limit 2 offset 2 order by field ASC
$users = $model->find()->limit(2)->offset(2)->order("campo_da_tabela ASC")->fetch(true);
// find all users with in operator
$users = $model->find()->in("id", [1, 2, 3])->fetch(true);
//looping users
foreach ($users as $user) {
echo $user->campo_da_tabela;
}
//find one user by condition
$user = $model->find("campo_da_tabela = :name", "name=Paulinho")->fetch();
echo $user->campo_da_tabela;
//find one user by two conditions
$user = $model->find("campo_da_tabela = :name AND last_name = :last", "name=Paulinho&last=Supriano")->fetch();
echo $user->campo_da_tabela . " " . $user->first_last;
//find one user by condition and with in operator
$user = $model->find("campo_da_tabela = :name", "name=Paulinho")->in("last_name",["Menezes", "Sampaio"])->fetch(true);
foreach ($users as $user) {
echo $user->campo_da_tabela . " " . $user->first_last;
}
use Example\Models\User;
$model = new User();
$user = $model->findById(2);
echo $user->campo_da_tabela;