PHP code example of phore / orm

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

    

phore / orm example snippets


namespace App\Entity;

use Phore\MiniSql\Schema\OrmClassSchema;

class User
{
    public int $id;
    public string $name;
    public string $email;

    public static function __schema(): OrmClassSchema
    {
        return new OrmClassSchema(
            tableName: 'users',
            primaryKey: 'id',
            autoincrement: true,
            columns: [
                'id' => 'int',
                'name' => 'varchar(255)',
                'email' => 'varchar(255)'
            ]
        );
    }
}

use Phore\MiniSql\Orm;
use App\Entity\User;

$orm = new Orm([User::class], 'mysql:host=localhost;dbname=testdb;user=root;password=root');
$orm->connect();

$user = new User();
$user->name = 'John Doe';
$user->email = '[email protected]';
$orm->create($user);

$user = $orm->withClass(User::class)->read(1);

$user->name = 'Jane Doe';
$orm->update($user);

$orm->delete($user);

$users = $orm->withClass(User::class)->listAll();

$users = $orm->withClass(User::class)->select(['name' => 'Jane Doe']);

public static function __schema(): OrmClassSchema
{
    return new OrmClassSchema(
        tableName: 'users',
        primaryKey: 'id',
        autoincrement: true,
        columns: [
            'id' => 'int',
            'name' => 'varchar(255)',
            'email' => 'varchar(255)'
        ],
        indexes: [
            'idx_name' => ['name'],
            'idx_email' => ['email']
        ]
    );
}

use Phore\MiniSql\Schema\OrmForeignKey;

public static function __schema(): OrmClassSchema
{
    return new OrmClassSchema(
        tableName: 'orders',
        primaryKey: 'id',
        autoincrement: true,
        columns: [
            'id' => 'int',
            'user_id' => 'int',
            'product_id' => 'int'
        ],
        foreignKeys: [
            new OrmForeignKey('user_id', 'users', 'id'),
            new OrmForeignKey('product_id', 'products', 'id')
        ]
    );
}

$orm->updateSchema();

$orm->getDriver()->getSchemaUpdater()->dropAllTables();