riverside / php-orm
PHP ORM micro-library and query builder
Fund package maintenance!
www.paypal.me/Dimitar81
Installs: 974
Dependents: 0
Suggesters: 0
Security: 0
Stars: 11
Watchers: 1
Forks: 2
Open Issues: 2
pkg:composer/riverside/php-orm
Requires
- php: >=7.1
- ext-pdo: *
Requires (Dev)
README
PHP micro-ORM and query builder.
| Build | GitHub pages | Stable | License |
|---|---|---|---|
Requirements
- PHP >= 7.1
- PHP extensions:
- PDO (
ext-pdo)
- 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
| Name | Type | Collation | Attributes | Null | Extra |
|---|---|---|---|---|---|
| id | int(10) | UNSIGNED | No | AUTO_INCREMENT | |
| name | varchar(255) | utf8mb4_general_ci | Yes | ||
| varchar(255) | utf8mb4_general_ci | Yes |
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
factorymethod. 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\DBclass
$db = new \Riverside\Orm\DB(); $db->table('users')->get();