Skip to content

Latest commit

 

History

History
209 lines (155 loc) · 4.34 KB

orm.md

File metadata and controls

209 lines (155 loc) · 4.34 KB
layout title order repo
page
ORM
4
orm

ORM, based on medoo.in

Install

composer require wtf/orm

Configuration

Create file medoo.php into your config dir with the following content:

<?php

declare(strict_types=1);

return [
    'namespace' => '\App\Entity\\',
    'database_type' => 'mysql',
    'database_name' => 'tisuit',
    'server' => '127.0.0.1',
    'username' => 'travis',
    'password' => 'secret_password',
    'charset' => 'utf8',
    'port' => 3306,
    'option' => [
        PDO::ATTR_CASE => PDO::CASE_NATURAL,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ],
];

And file phinx.php into your config dir with the following content:

<?php

declare(strict_types=1);
$db = require __DIR__.'/medoo.php';

return [
    'paths' => [
        'migrations' => dirname(__DIR__).'/migrations',
        'seeds' => dirname(__DIR__).'/seeds',
    ],

    'environments' => [
        'default_database' => 'default',
        'default' => [
            'name' => $db['database_name'],
            'host' => $db['server'],
            'user' => $db['username'],
            'pass' => $db['password'],
            'port' => $db['port'],
            'charset' => $db['charset'],
            'adapter' => $db['database_type'],
        ],
    ],
];

Add \Wtf\ORM\Provider into your providers list (suit.php)

Usage

Get entity

From any \Wtf\Root child class

Load one entry

<?php

$authorById = $this->entity('author')->load($id);

$authorByProperty = $this->entity('author')->load('[email protected]', 'email');

Load collection

<?php

//Load collection as relation of author
$author = $this->entity('author')->load($id);
$books = $author->getBooks();

//Load collection with WHERE filter
$books = $this->entity('book')->loadAll(['author_id' => $id]);

Entity

This library provides you \TiSuit\ORM\Entity abstract class, which must be used as parent class of your project entities.

Each entity has the following abstract methods:

public function getTable(): string

This function MUST return entity table name in database

public function getValidators(): array

Return array of respect/validation rules.

NOTE: If you don't need validations, just return an empty array

Structure:

<?php
return [
    '<method_name>' => [
        '<field_name>' => <rule>
    ],
];

Example:

<?php
use Respect\Validation\Validator as v;
return [
    'save' => [ //method name, default: save (will be called on Entity::save())
        'name' => v::stringType()->length(1,255),
    ],
];

public function getRelations(): array

Return array of relations with other entities

NOTE: If you don't need relations, just return an empty array

Structure:

<?php
return [
    '<relation_name>' => [
        'entity' => '<related_entity_name>',
        'type' => 'has_one', //default, other options: has_many
        'key' => 'current_entity_key', //optional, default for has_one: <current_entity>_id, for has_many: id
        'foreign_key' => 'another_entity_key', //optional, default for has_one: id, for has_many: '<current_entity>_id'
        'assoc' => true, //optional, load assoc data arrays instead of objects for has_many type. Default: false
    ],
];

Example (current entity: blog post, another entity: user):

<?php
return [
    'author' => [ //has_one
        'entity' => 'user',
        'key' => 'author_id',
        'foreign_key' => 'id'
    ],
];

Example (same as above, but with defaults):

<?php
return [
    'author' => [ //has_one
        'entity' => 'user',
    ],
];

This example can be called $blogPostEntity->getAuthor()

Migrations and Seeds

Just use phinx.

Phinx config file (for wtf): app/config/phinx.php

Documentation