riverside / php-orm
PHP ORM micro-library and query builder
Fund package maintenance!
www.paypal.me/Dimitar81
Requires
- php: >=7.1
- ext-pdo: *
Requires (Dev)
README
PHP micro-ORM and query builder.
Build | GitHub pages | Stable | 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 | ||
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();