riverside/php-orm

PHP ORM micro-library and query builder

2.0.0 2024-09-14 17:01 UTC

This package is auto-updated.

Last update: 2024-11-14 17:28:59 UTC


README

PHP micro-ORM and query builder.

Requirements

  • PHP >= 7.1
  • PHP extensions:
    • PDO (ext-pdo)

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" : "^2.0"
    }
}

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';

Configure database credentials by setting up the following environment variables: USERNAME, PASSWORD, DATABASE, HOST, PORT, DRIVER, CHARSET, COLLATION. They must be prefixed with $connection property value, part of your model, in capital letters and underscore. The default value of $connection property is 'default'. For example:

<?php
putenv("DEFAULT_USERNAME=your_username");
putenv("DEFAULT_PASSWORD=your_password");
putenv("DEFAULT_DATABASE=your_database");
putenv("DEFAULT_HOST=localhost");
putenv("DEFAULT_PORT=3306");
putenv("DEFAULT_DRIVER=mysql");
putenv("DEFAULT_CHARSET=utf8mb4");
putenv("DEFAULT_COLLATION=utf8mb4_general_ci");

Drivers

The following drivers are supported: mysql, oci, firebird, pgsql, sqlite, sybase, mssql, dblib, cubrid, 4D.

Database

Table: users

Models

Define your own models:

<?php
use Riverside\Orm\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. The following is example of simple CRUD:
$id = User::factory()->insert(['name' => 'Chris', 'email' => 'chris@aol.com']);
$user = User::factory()->where('id', $id)->get();
$affected_rows = User::factory()->where('id', $id)->update(['name' => 'Chris Johnson']);
$affected_rows = User::factory()->where('id', $id)->delete();
  • without model: create a new instance of Riverside\Orm\DB class
$db = new \Riverside\Orm\DB();
$db->table('users')->get();

API