|
3 | 3 |
|
4 | 4 | ORM, based on [medoo.in](http://medoo.in)
|
5 | 5 |
|
6 |
| -## Table of Contents |
7 |
| - |
8 |
| - |
9 |
| -<!-- vim-markdown-toc GFM --> |
10 |
| - |
11 |
| -* [Installation](#installation) |
12 |
| - - [Composer](#composer) |
13 |
| - - [Configuration](#configuration) |
14 |
| -* [Usage](#usage) |
15 |
| - - [Get entity](#get-entity) |
16 |
| - - [Entity](#entity) |
17 |
| - - [Migrations and Seeds](#migrations-and-seeds) |
18 |
| -* [Documentation](#documentation) |
19 |
| - |
20 |
| -<!-- vim-markdown-toc --> |
21 |
| - |
22 |
| -## Installation |
23 |
| - |
24 |
| -### Composer |
25 |
| - |
26 |
| -```bash |
27 |
| -composer require wtf/orm |
28 |
| -``` |
29 |
| - |
30 |
| -### Configuration |
31 |
| - |
32 |
| -Create file `medoo.php` into your config dir with following contents: |
33 |
| - |
34 |
| -```php |
35 |
| -<?php |
36 |
| - |
37 |
| -declare(strict_types=1); |
38 |
| - |
39 |
| -return [ |
40 |
| - 'namespace' => '\App\Entity\\', |
41 |
| - 'database_type' => 'mysql', |
42 |
| - 'database_name' => 'tisuit', |
43 |
| - 'server' => '127.0.0.1', |
44 |
| - 'username' => 'travis', |
45 |
| - 'password' => 'secret_password', |
46 |
| - 'charset' => 'utf8', |
47 |
| - 'port' => 3306, |
48 |
| - 'option' => [ |
49 |
| - PDO::ATTR_CASE => PDO::CASE_NATURAL, |
50 |
| - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
51 |
| - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, |
52 |
| - ], |
53 |
| -]; |
54 |
| -``` |
55 |
| - |
56 |
| -And file `phinx.php` into your config dir with following contents: |
57 |
| - |
58 |
| -```php |
59 |
| -<?php |
60 |
| - |
61 |
| -declare(strict_types=1); |
62 |
| -$db = require __DIR__.'/medoo.php'; |
63 |
| - |
64 |
| -return [ |
65 |
| - 'paths' => [ |
66 |
| - 'migrations' => dirname(__DIR__).'/migrations', |
67 |
| - 'seeds' => dirname(__DIR__).'/seeds', |
68 |
| - ], |
69 |
| - |
70 |
| - 'environments' => [ |
71 |
| - 'default_database' => 'default', |
72 |
| - 'default' => [ |
73 |
| - 'name' => $db['database_name'], |
74 |
| - 'host' => $db['server'], |
75 |
| - 'user' => $db['username'], |
76 |
| - 'pass' => $db['password'], |
77 |
| - 'port' => $db['port'], |
78 |
| - 'charset' => $db['charset'], |
79 |
| - 'adapter' => $db['database_type'], |
80 |
| - ], |
81 |
| - ], |
82 |
| -]; |
83 |
| -``` |
84 |
| - |
85 |
| -Add `\Wtf\ORM\Provider` into your providers list (`suit.php`) |
86 |
| - |
87 |
| -## Usage |
88 |
| - |
89 |
| -### Get entity |
90 |
| - |
91 |
| -From any `\Wtf\Root` child class |
92 |
| - |
93 |
| -**Load one entry** |
94 |
| - |
95 |
| -```php |
96 |
| -<?php |
97 |
| - |
98 |
| -$authorById = $this->entity('author')->load($id); |
99 |
| - |
100 |
| -$authorByProperty = $this->entity('author')->load(' [email protected]', 'email'); |
101 |
| -``` |
102 |
| - |
103 |
| -**Load collection** |
104 |
| - |
105 |
| -```php |
106 |
| -<?php |
107 |
| - |
108 |
| -//Load collection as relation of author |
109 |
| -$author = $this->entity('author')->load($id); |
110 |
| -$books = $author->getBooks(); |
111 |
| - |
112 |
| -//Load collection with WHERE filter |
113 |
| -$books = $this->entity('book')->loadAll(['author_id' => $id]); |
114 |
| -``` |
115 |
| - |
116 |
| -### Entity |
117 |
| - |
118 |
| -This library provides you `\TiSuit\ORM\Entity` abstract class, which must be used as parent class of your project entities. |
119 |
| - |
120 |
| -Each entity has following abstract methods: |
121 |
| - |
122 |
| -**public function getTable(): string** |
123 |
| - |
124 |
| -This function MUST return entity table name in database |
125 |
| - |
126 |
| -**public function getValidators(): array** |
127 |
| - |
128 |
| -Return array of [respect/validation](https://github.com/Respect/Validation) rules. |
129 |
| - |
130 |
| -> NOTE: If you don't need validations, just return empty array |
131 |
| -
|
132 |
| -Structure: |
133 |
| - |
134 |
| -```php |
135 |
| -<?php |
136 |
| -return [ |
137 |
| - '<method_name>' => [ |
138 |
| - '<field_name>' => <rule> |
139 |
| - ], |
140 |
| -]; |
141 |
| -``` |
142 |
| - |
143 |
| -Example: |
144 |
| - |
145 |
| -```php |
146 |
| -<?php |
147 |
| -use Respect\Validation\Validator as v; |
148 |
| -return [ |
149 |
| - 'save' => [ //method name, default: save (will be called on Entity::save()) |
150 |
| - 'name' => v::stringType()->length(1,255), |
151 |
| - ], |
152 |
| -]; |
153 |
| -``` |
154 |
| - |
155 |
| -**public function getRelations(): array** |
156 |
| - |
157 |
| -Return array of relations with other entities |
158 |
| - |
159 |
| -> NOTE: If you don't need relations, just return empty array |
160 |
| -
|
161 |
| -Structure: |
162 |
| - |
163 |
| -```php |
164 |
| -<?php |
165 |
| -return [ |
166 |
| - '<relation_name>' => [ |
167 |
| - 'entity' => '<related_entity_name>', |
168 |
| - 'type' => 'has_one', //default, other options: has_many |
169 |
| - 'key' => 'current_entity_key', //optional, default for has_one: <current_entity>_id, for has_many: id |
170 |
| - 'foreign_key' => 'another_entity_key', //optional, default for has_one: id, for has_many: '<current_entity>_id' |
171 |
| - ], |
172 |
| -]; |
173 |
| -``` |
174 |
| -Example (current entity: blog post, another entity: user): |
175 |
| - |
176 |
| -```php |
177 |
| -<?php |
178 |
| -return [ |
179 |
| - 'author' => [ //has_one |
180 |
| - 'entity' => 'user', |
181 |
| - 'key' => 'author_id', |
182 |
| - 'foreign_key' => 'id' |
183 |
| - ], |
184 |
| -]; |
185 |
| -``` |
186 |
| - |
187 |
| -Example (same as above, but with defaults): |
188 |
| - |
189 |
| -```php |
190 |
| -<?php |
191 |
| -return [ |
192 |
| - 'author' => [ //has_one |
193 |
| - 'entity' => 'user', |
194 |
| - ], |
195 |
| -]; |
196 |
| -``` |
197 |
| - |
198 |
| -This example can be called like `$blogPostEntity->getAuthor()` |
199 |
| - |
200 |
| -### Migrations and Seeds |
201 |
| - |
202 |
| -Just use phinx. |
203 |
| - |
204 |
| -Phinx config file (for wtf): `app/config/phinx.php` |
205 |
| - |
206 |
| -## Documentation |
207 |
| - |
208 |
| -* [medoo](https://medoo.in) - wrapper around PDO, used for DB manipulation |
209 |
| -* [respect/validation](https://github.com/Respect/Validation) - validation library |
210 |
| -* [phinx](https://phinx.org) - DB migrations the right way |
| 6 | +[Documentation](https://framework.wtf/orm) |
0 commit comments