Enjoin is an active-record ORM for PHP. Enjoin built on Laravel Components and inspired by Sequelize for Node.js. It supports MySQL and PostgreSQL dialects. Unlike Eloquent, Enjoin features eager loading, built-in validation and more.
Enjoin is available via Composer:
$ composer require mightydes/enjoin
If you need language locales, then require laravel-lang package:
$ composer require caouecs/laravel-lang
Then add Models
autoload entry to your composer.json
file:
"autoload": {
"psr-4": {
"Models\\": [
"app/Models"
]
}
}
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Enjoin\Factory;
Factory::bootstrap([
'database' => [
'default' => 'acme',
'connections' => [
'acme' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'acme',
'username' => 'acme',
'password' => 'acme',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
]
],
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
]
]
],
'enjoin' => [
'lang_dir' => 'vendor/caouecs/laravel-lang'
],
'cache' => [
'default' => 'redis',
'stores' => [
'redis' => [
'driver' => 'redis',
'connection' => 'default'
]
],
'prefix' => 'enjoin_'
]
]);
First add Enjoin\EnjoinServiceProvider
to providers
list,
and 'Enjoin' => 'Enjoin\EnjoinFacade'
to aliases
list in config/app.php
.
Then register config in app/Providers/ConfigServiceProvider.php
:
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ConfigServiceProvider extends ServiceProvider
{
public function register()
{
config([
'enjoin' => [
'lang_dir' => 'vendor/caouecs/laravel-lang'
]
]);
}
}
You need to define mapping between tables and models.
For example, table books
with columns:
- id (int, primary, auto-increment)
- authors_id (int, foreign-key)
- title (varchar)
- year (int)
Has definition:
<?php namespace Models;
use Enjoin\Model\Definition;
use Enjoin\Enjoin;
class Books extends Definition
{
public function getAttributes()
{
return [
'id' => ['type' => Enjoin::Integer()],
'authors_id' => ['type' => Enjoin::Integer()],
'title' => ['type' => Enjoin::String()],
'year' => ['type' => Enjoin::Integer(), 'validate' => 'integer|max:2020']
];
}
public function getRelations()
{
return [
Enjoin::belongsTo(Enjoin::get('Authors'), ['foreignKey' => 'authors_id'])
];
}
}
Connection to use. Equals to default
connection by default.
Example:
<?php namespace Models;
use Enjoin\Model\Definition;
use Enjoin\Enjoin;
class Books extends Definition
{
public $connection = 'acme';
}
Table name. Equals to Inflector::tableize(<ModelClassName>)
by default.
Example:
<?php namespace Models;
use Enjoin\Model\Definition;
use Enjoin\Enjoin;
class Books extends Definition
{
public $table = 'books';
}
Enables or disables timestamps (ie created_at
, updated_at
). Equals to true
by default.
Example:
<?php namespace Models;
use Enjoin\Model\Definition;
use Enjoin\Enjoin;
class Books extends Definition
{
public $timestamps = false;
}
CreatedAt column name. Equals to created_at
by default.
Example:
<?php namespace Models;
use Enjoin\Model\Definition;
use Enjoin\Enjoin;
class Books extends Definition
{
public $createdAt = 'createdAt';
}
UpdatedAt column name. Equals to updated_at
by default.
Example:
<?php namespace Models;
use Enjoin\Model\Definition;
use Enjoin\Enjoin;
class Books extends Definition
{
public $updatedAt = 'updatedAt';
}
Enables or disables cache. Equals to false
by default.
Example:
<?php namespace Models;
use Enjoin\Model\Definition;
use Enjoin\Enjoin;
class Books extends Definition
{
public $cache = true;
}
Class name to extend generic model class. It is useful, if you want to define custom model methods. Example:
<?php namespace Models;
use Enjoin\Model\Definition;
use Enjoin\Enjoin;
class Books extends Definition
{
public $expanseModel = Expanse\BooksModel::class;
}
Class name to extend generic record class. It is useful, if you want to define custom record methods. Example:
<?php namespace Models;
use Enjoin\Model\Definition;
use Enjoin\Enjoin;
class Books extends Definition
{
public $expanseRecord = Expanse\BooksRecord::class;
}
Returns associative array, where each key is a table column.
- Enjoin::Integer()
- Enjoin::Boolean()
- Enjoin::String()
- Enjoin::Text()
- Enjoin::Float()
- Enjoin::Date()
- Enjoin::Enum()
Indicates is NULL
allowed on field.
By default is true
.
Validation rules for field. You can read more about validation here.
You can define custom getter closure for field:
'fullname' => [
'type' => Enjoin::String(),
'get' => function ($attr, Closure $getValue) {
$v = $getValue($attr);
if (is_string($v) && strlen($v) > 0) {
$r = explode(' ', $v);
return [
'name' => $r[0],
'surname' => $r[1]
];
}
return null;
}
]
You can define custom setter closure for field:
'fullname' => [
'type' => Enjoin::String(),
'set' => function ($attr, Closure $getValue) {
$v = $getValue($attr);
if (is_array($v)) {
return join(' ', $v);
}
return $v;
}
]
Returns array, where each value is a relation. Supported relations:
belongsTo
hasMany
hasOne
Example:
public function getRelations()
{
return [
Enjoin::belongsTo(Enjoin::get('Authors')),
Enjoin::belongsTo(Enjoin::get('Languages')),
Enjoin::hasMany(Enjoin::get('Reviews')),
Enjoin::hasMany(Enjoin::get('PublishersBooks'))
];
}
Foreign key column name. Example:
public function getRelations()
{
return [
Enjoin::belongsTo(Enjoin::get('Authors'), ['foreignKey' => 'authors_id'])
];
}
Foreign key column as
alias.
Example:
public function getRelations()
{
return [
Enjoin::belongsTo(Enjoin::get('Authors'), ['as' => 'person'])
];
}