Skip to content

Commit 7174f0a

Browse files
committed
Update README.md
1 parent 7e5ef8b commit 7174f0a

File tree

1 file changed

+292
-2
lines changed

1 file changed

+292
-2
lines changed

README.md

Lines changed: 292 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,295 @@
22

33
The lightweight PHP ORM
44

5-
LightQL is a minimal database management class, which will help you communicate with
6-
your preferred DBMS in PHP through PDO.
5+
LightQL is an Object Oriented Mapper (ORM) based on [Query Object](http://martinfowler.com/eaaCatalog/queryObject.html)
6+
and [Data Mapper](https://martinfowler.com/eaaCatalog/dataMapper.html) design patterns.
7+
It uses [Annotations](https://github.com/ElementaryFramework/Annotations) to help you
8+
create, edit, delete, find entities and much more without writing a SQL query.
9+
10+
## Example
11+
12+
### 1. Create a persistence unit
13+
14+
```json
15+
{
16+
"DBMS": "mysql",
17+
"Hostname": "127.0.0.1",
18+
"DatabaseName": "my_app_db",
19+
"Username": "root",
20+
"Password": ""
21+
}
22+
```
23+
24+
### 2. Create entities
25+
```php
26+
<?php
27+
28+
namespace MyApp\Entities;
29+
30+
/**
31+
* Class UserEntity
32+
*
33+
* @entity('table' => 'users', 'fetchMode' => \ElementaryFramework\LightQL\Entities\Entity::FETCH_EAGER)
34+
* @namedQuery('name' => 'findAll', 'query' => 'SELECT * FROM users')
35+
* @namedQuery('findById', 'SELECT * FROM users u WHERE u.user_id = :id')
36+
*/
37+
class UserEntity extends \ElementaryFramework\LightQL\Entities\Entity
38+
{
39+
/**
40+
* @id
41+
* @autoIncrement
42+
* @column('name' => 'user_id', 'type' => 'int')
43+
* @size(11)
44+
* @notNull
45+
*
46+
* @var int
47+
*/
48+
public $ID = null;
49+
50+
/**
51+
* @column('name' => 'first_name', 'type' => 'string')
52+
* @size(255)
53+
* @notNull
54+
*
55+
* @var string
56+
*/
57+
public $firstName = null;
58+
59+
/**
60+
* @column('name' => 'last_name', 'type' => 'string')
61+
* @size(255)
62+
* @notNull
63+
*
64+
* @var string
65+
*/
66+
public $lastName = null;
67+
68+
/**
69+
* @column('name' => 'login', 'type' => 'string')
70+
* @size(15)
71+
* @notNull
72+
*
73+
* @var string
74+
*/
75+
public $login = null;
76+
77+
/**
78+
* @manyToOne(
79+
* 'entity' => 'TopicEntity',
80+
* 'column' => 'user_id',
81+
* 'referencedColumn' => 'author_id'
82+
* )
83+
*
84+
* @var TopicEntity[]
85+
*/
86+
public $topicEntityCollection;
87+
}
88+
```
89+
90+
```php
91+
<?php
92+
93+
namespace MyApp\Entities;
94+
95+
/**
96+
* Class TopicEntity
97+
*
98+
* @entity('table' => 'topics')
99+
* @namedQuery('name' => 'findAll', 'query' => 'SELECT * FROM topics')
100+
* @namedQuery('findById', 'SELECT * FROM topics t WHERE t.topic_id = :id')
101+
* @namedQuery('findByUser', 'SELECT * FROM topics t WHERE t.author_id = :id')
102+
*/
103+
class TopicEntity extends \ElementaryFramework\LightQL\Entities\Entity
104+
{
105+
/**
106+
* @id
107+
* @autoIncrement
108+
* @column('name' => 'topic_id', 'type' => 'int')
109+
* @size(11)
110+
* @notNull
111+
*
112+
* @var int
113+
*/
114+
public $ID = null;
115+
116+
/**
117+
* @column('name' => 'title', 'type' => 'string')
118+
* @size(255)
119+
* @notNull
120+
*
121+
* @var string
122+
*/
123+
public $title = null;
124+
125+
/**
126+
* @column('name' => 'content', 'type' => 'string')
127+
* @notNull
128+
*
129+
* @var string
130+
*/
131+
public $text = null;
132+
133+
/**
134+
* @oneToMany('entity' => 'UserEntity')
135+
*
136+
* @var UserEntity
137+
*/
138+
public $userEntityReference;
139+
}
140+
```
141+
142+
### 3. Create entity Facades
143+
144+
```php
145+
<?php
146+
147+
namespace MyApp\Sessions;
148+
149+
use MyApp\Entities\UserEntity;
150+
151+
/**
152+
* Class UserFacade
153+
*/
154+
class UserFacade extends \ElementaryFramework\LightQL\Sessions\Facade
155+
{
156+
/**
157+
* @persistenceUnit('myAppPersistenceUnit')
158+
*
159+
* @var \ElementaryFramework\LightQL\Entities\EntityManager
160+
*/
161+
protected $entityManager;
162+
163+
public function __construct()
164+
{
165+
// Constructs the base class with the entity class name managed by this facade
166+
parent::__construct(UserEntity::class);
167+
}
168+
}
169+
```
170+
171+
```php
172+
<?php
173+
174+
namespace MyApp\Sessions;
175+
176+
use MyApp\Entities\TopicEntity;
177+
178+
/**
179+
* Class TopicFacade
180+
*/
181+
class TopicFacade extends \ElementaryFramework\LightQL\Sessions\Facade
182+
{
183+
/**
184+
* @persistenceUnit('myAppPersistenceUnit')
185+
*
186+
* @var \ElementaryFramework\LightQL\Entities\EntityManager
187+
*/
188+
protected $entityManager;
189+
190+
public function __construct()
191+
{
192+
// Constructs the base class with the entity class name managed by this facade
193+
parent::__construct(TopicEntity::class);
194+
}
195+
}
196+
```
197+
198+
### 4. Play the game !
199+
200+
```php
201+
<?php
202+
203+
namespace MyApp\Controllers;
204+
205+
use ElementaryFramework\LightQL\LightQL;
206+
use ElementaryFramework\LightQL\Persistence\PersistenceUnit;
207+
208+
abstract class BaseController
209+
{
210+
public function __construct()
211+
{
212+
// Register LightQL annotations
213+
LightQL::registerAnnotations();
214+
215+
// Register persistence unit
216+
PersistenceUnit::register("myAppPersistenceUnit", __DIR__ . "/files/persistenceUnit.json");
217+
}
218+
}
219+
```
220+
221+
```php
222+
<?php
223+
224+
namespace MyApp\Controllers;
225+
226+
use MyApp\Entities\TopicEntity;
227+
use MyApp\Sessions\TopicFacade;
228+
229+
class TopicController extends BaseController
230+
{
231+
private $_topicFacade;
232+
233+
public function __construct()
234+
{
235+
parent::__construct();
236+
237+
// Create a new facade
238+
$this->_topicFacade = new TopicFacade();
239+
}
240+
241+
public function newTopic()
242+
{
243+
// Create a topic entity from form data
244+
$topic = new TopicEntity($_POST);
245+
// Insert the entity into the database
246+
$this->_topicFacade->create($topic);
247+
}
248+
249+
public function editTopic()
250+
{
251+
// Get the original topic from the database
252+
$topic = $this->_topicFacade->find($_POST["topic_id"]);
253+
// Edit the topic with form data
254+
$topic->hydrate($_POST);
255+
// Update the data in the database
256+
$this->_topicFacade->edit($topic);
257+
}
258+
259+
public function getTopics($start = null, $length = null)
260+
{
261+
$topics = array();
262+
263+
if ($start === null && $length === null) {
264+
$topics = $this->_topicFacade->findAll();
265+
} else {
266+
$topics = $this->_topicFacade->findRange($start, $length);
267+
}
268+
269+
$this->renderView("topics_page", array("topics" => $topics));
270+
}
271+
272+
public function getTopicsOfUser($userId)
273+
{
274+
// Get the named query
275+
$query = $this->_topicFacade->getNamedQuery("findByUser");
276+
// Set query parameters
277+
$query->setParam("id", $userId);
278+
// Execute the query
279+
$query->run();
280+
// Retrieve results
281+
$topics = $query->getResults();
282+
283+
$this->renderView("topics_page", array("topics" => $topics));
284+
}
285+
286+
// etc...
287+
}
288+
```
289+
290+
You can do the same with an `UserController`
291+
292+
## License
293+
294+
&copy; 2018 - Aliens Group
295+
296+
Licensed under MIT ([read license](https://github.com/ElementaryFramework/LightQL/blob/master/LICENSE))

0 commit comments

Comments
 (0)