riverside/php-orm

PHP ORM micro-library and query builder

1.1.0 2023-06-24 10:03 UTC

This package is auto-updated.

Last update: 2024-04-10 07:38:39 UTC


README

PHP micro-ORM and query builder.

Build GitHub pages Stable License
CI pages-build-deployment Latest Stable Version License

Requirements

  • PHP >= 7.1
  • PDO extension

Installation

If Composer is not installed on your system yet, you may go ahead and install it using this command line:

$ curl -sS https://getcomposer.org/installer | php

Next, add the following require entry to the composer.json file in the root of your project.

{
    "require" : {
        "riverside/php-orm" : "*"
    }
}

Finally, use Composer to install php-orm and its dependencies:

$ php composer.phar install 

Configuration

Include autoload in your project:

require __DIR__ . '/vendor/autoload.php';

Define path to configuration file:

DB::config('config/database.php');

config/database.php

<?php
return array(
    'default' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'port'      => 3306,
        'username'  => 'root',
        'password'  => 'secret',
        'database'  => 'test',
        'charset'   => 'utf8mb4',
        'collation' => 'utf8mb4_general_ci',
    ),
);

Database

Table: users

Name Type Collation Attributes Null Extra
id int(10) UNSIGNED No AUTO_INCREMENT
name varchar(255) utf8mb4_general_ci Yes
email varchar(255) utf8mb4_general_ci Yes

Models

Define your own models:

<?php
use PhpOrm\DB;

class User extends DB
{
    protected $table = 'users';
    
    protected $attributes = ['id', 'name', 'email'];
    
    // protected $connection = 'backup';
    
    public static function factory()
    {
        return new self();
    }
}

Query Builder

  • with model: create an instance of a model using the factory method. Then chain multiple methods.
User::factory()->get();
  • without model: create a new instance of PhpOrm\DB class
$db = new \PhpOrm\DB();
$db->table('users')->get();

API