PHP code example of erodriguezds / active-orm

1. Go to this page and download the library: Download erodriguezds/active-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/ */

    

erodriguezds / active-orm example snippets


$results = (new Query($myPdo))
    ->select('*') // not necesary
    ->from('users')
    ->where([
        'name' => $submitedName, // translates to "name = ? ", where '?' will be replaced by the SQL-escaped value of $submittedPage
        [ 'age', '>', $submitedAge ], // 3-elements array syntax for 'field', 'operator' and 'value'. The 3rd element will be prepared by PDO
        "age < $submitedAge", //more confortable, but this way, you won't have a PDO prepared parameter
        'id' => [2, 3, 6], // translates to "id IN(2, 3, 6)"
    ], 'OR') // OR's all the previously defined condition. Default is 'AND'
    ->fetchAll(); // you could also "fetchOne()"
    
foreach($result as $row){
    // ...
}


use ActiveORM\Query;
....
$pdo = new \PDO('mysql:dbname=goldoni;host=localhost;charset=utf8', 'root', 'root');
$query = (new Query($pdo))

use ActiveORM\Query;
....
$pdo = new \PDO('mysql:dbname=goldoni;host=localhost;charset=utf8', 'root', 'root');
Query::setPdo($pdo);
....
$query = (new Query()) //will get the PDO instance from the global set
    ->select('*')
    ->from('users')
    ->where(...)


composer 

$query = (new Query())->from('users')->select('first_name');

$query = (new Query($this->pdo))
            ->insert(
                'users',
                [
                    'first_name' => ':first_name',
                    'last_name'  => ':last_name',
                    'email'      => ':email',
                    'mobile'     => ':mobile',
                    'phone'      => ':phone',
                ]
            );
            
$query1 = (new Query($this->pdo))
            ->insert('users')
            ->value([
                'first_name' => ':first_name',
                'last_name'  => ':last_name',
                'email'      => ':email',
                'phone'      => ':phone'
            ]);

$data = [
    'id' => 12,
    'first_name' => 'Joe3',
    'last_name'  => 'Doe3',
    'email'      => '[email protected]',
    'phone'      => '+0172222222'
];

$query = (new Query($this->pdo))
    ->insert(
        'users',
        [
            'id' => ':id',
            'first_name' => ':first_name',
            'last_name'  => ':last_name',
            'email'      => ':email',
            'phone'      => ':phone'
        ]
    )->params($data)
    ->execute();

$data = [
    'first_name' => 'Joe3',
    'last_name'  => 'Doe3',
    'email'      => '[email protected]'
];
$query = (new Query($this->pdo))
        ->update(
            'users',
            [
                'first_name' => ':first_name',
                'last_name'  => ':last_name',
                'email'      => ':email'
            ],
            2
        )
        ->params($data);
            
$query1 = (new Query($this->pdo))
            ->update('users')
            ->set([
                'first_name' => ':first_name',
                'last_name'  => ':last_name',
                'email'      => ':email'
            ])
            ->where('id = :id')
            ->params($data);

$data = [
    'first_name' => 'Joe',
    'last_name'  => 'Doe',
    'email'      => '[email protected]',
    'phone'      => '+0172222222'
];
$query = (new Query($this->pdo))
    ->update(
        'users',
        [
            'first_name' => ':first_name',
            'last_name'  => ':last_name',
            'email'      => ':email',
            'phone'      => ':phone'
        ],
        4
    )
    ->params($data)
    ->execute();

$query = (new Query($this->pdo))->delete('users', 2);
            
$query1 = (new Query($this->pdo))->delete('users')->where('id = :id')->params(['id' => 12]);

(new Query($this->pdo))->delete('users', 2)->execute();

$query = (new Query())
            ->from('users', 'u')
            ->where('first_name = :first_name OR email = :email', 'phone = :phone');
            
$query2 = (new Query())
            ->from('users', 'u')
            ->where('first_name = :first_name OR email = :email')
            ->where('mobile = :mobile');

$user = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('id = :id')
            ->params(['id' => 1]);

$usersCount = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('u.id < :number')
            ->params([
                'number' => 5
            ])
            ->count();

$query = (new Query($this->pdo))
            ->from('users', 'u')
            ->orderBy('u.id', 'DESC');

$query = (new Query())
            ->select('u.first_name', 'COUNT(id)')
            ->from('users', 'u')
            ->groupBy('u.updated_at');

 $query = (new Query())
            ->from('users', 'u')
            ->select('first_name')
            ->join('posts as p', 'u.id = p.user_id');
            
 $query = (new Query())
            ->from('users', 'u')
            ->select('first_name')
            ->join('posts as p2', 'u.id = p2.user_id', 'inner');

$query = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('first_name = :first_name OR email = :email')
            ->limit(5);

$query = (new Query($this->pdo))->from('users')->select('first_name')->fetchAll();

$user = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('id = :id')
            ->params(['id' => 1])
            ->fetch();

$user = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('id = :id')
            ->params(['id' => 100])
            ->fetchOrFail();

$query = (new Query($this->pdo))
            ->insert(
                'users',
                [
                    'first_name' => ':first_name',
                    'last_name'  => ':last_name',
                    'email'      => ':email',
                    'mobile'     => ':mobile',
                    'phone'      => ':phone',
                ]
            )
            ->execute();

    $paginate = (new Query($this->pdo))
        ->from('users', 'u')
        ->into(Demo::class)
        ->paginate(5, 1);
            
    $paginate->getNbPages();
    $paginate->haveToPaginate();
    $paginate->hasPreviousPage();
    $paginate->getPreviousPage();
    $paginate->hasNextPage();
    $paginate->getNextPage();
    $paginate->getCurrentPageOffsetStart();
    $paginate->getCurrentPageOffsetEnd();
    $paginate->getIterator(); // return collections of objects

use Goldoni\Builder\Entities\Demo;
...
$demos = (new Query($this->pdo))
            ->from('users', 'u')
            ->into(Demo::class)
            ->fetchAll();

$demo1 = $demos[0]; 

// get_class($demo1) === Demo::class  

echo $demo1->firstName; 

$demo2 = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('id = :id')
            ->into(Demo::class)
            ->params(['id' => 2])
            ->fetch();
            
// get_class($demo2) === Demo::class      

echo $demo2->firstName;

var_dump($demo1 instanceof Demo::class); // TRUE
var_dump($demo2 instanceof Demo::class); // TRUE