PHP code example of movescode / model

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

    

movescode / model example snippets


use MovesCode\Model\Connection;

$pdo = new PDO(
    'mysql:host=localhost;dbname=app;charset=utf8mb4',
    'usuario',
    'senha'
);

Connection::configure($pdo);

use MovesCode\Model\Model;

final class User extends Model
{
    public function __construct()
    {
        parent::__construct(
            entity: 'users',
            protected: ['id'],
            

$users = (new User())
    ->find('status = :status', ['status' => 'active'])
    ->order('name ASC')
    ->limit(20)
    ->offset(0)
    ->fetch(true);

$user = (new User())
    ->find('email = :email', http_build_query(['email' => $email]))
    ->fetch();

$user = (new User())->findById(10);
$selected = (new User())->find()->in('id', [1, 2, 3])->fetch(true);
$total = (new User())->find('status = :s', ['s' => 'active'])->count();

$user = new User();
$user->name = 'Ana';
$user->email = '[email protected]';

if (!$user->save()) {
    echo $user->message()->render();
}

$user->name = 'Ana Martins';
$user->save();

$user = (new User())->findById(10);
$user?->destroy();

(new User())->delete('status = :status', ['status' => 'inactive']);

$query = (new User())->find();
$total = $query->count();

$users = $query
    ->order('id DESC')
    ->limit($pager->limit())
    ->offset($pager->offset())
    ->fetch(true);

Connection::transaction(function (): void {
    $user = new User();
    $user->name = 'Ana';
    $user->email = '[email protected]';

    if (!$user->save()) {
        throw new RuntimeException('Falha ao salvar');
    }
});