1
1
# php-database
2
- A PHP library for accessing databases easily. The library provide a MySQL/MariaDB flavoured database object
2
+ A PHP library for accessing databases easily. This library provides a MySQL/MariaDB flavoured database object
3
3
that abstracts many daily task in SQL writing, such as quoting, escaping, building SQL statements, WHERE
4
4
clauses, error handling and so on.
5
5
@@ -67,7 +67,7 @@ $arr = $db->queryList('SELECT * FROM #__devtest');
67
67
$obj = $db->querySingle('SELECT * FROM #__devtest WHERE uid='.$uid);
68
68
```
69
69
70
- The interface usually delivers ` stdClass ` objects by default. However, you can name
70
+ The interface delivers ` stdClass ` objects by default. However, you can name
71
71
your own data class so the data will be populated in such a class:
72
72
73
73
```
@@ -147,7 +147,7 @@ $dao = \TgDatabase\DAO($db, '#__users');
147
147
The default constructor as above makes assumptions about your table:
148
148
149
149
1 . It always returns ` stdClass ` objects.
150
- 1 . It assumes that your table has an ` int auto-increment ` primary key that is names ` uid ` .
150
+ 1 . It assumes that your table has an ` int auto-increment ` primary key that is named ` uid ` .
151
151
152
152
However, you can tell ` DAO ` your specifics:
153
153
@@ -159,7 +159,8 @@ $dao = \TgDatabase\DAO($db, '#__users', 'MyNamespace\\User`);
159
159
$dao = \TgDatabase\DAO($db, '#__users', 'MyNamespace\\User`, 'id');
160
160
```
161
161
162
- ` DAO ` can actually handle non-numeric primary keys. The usage is not recommended though.
162
+ ` DAO ` can actually handle non-numeric primary keys. The usage is not recommended though as you need
163
+ to create the primary keys yourself.
163
164
164
165
## Finding objects
165
166
@@ -191,12 +192,12 @@ $newUser->group = 'webusers';
191
192
$newUser->active = 1;
192
193
$newId = $dao->create($newUser);
193
194
194
- // Updating an existing user
195
+ // Update an existing user
195
196
$user = $dao->get($newId);
196
197
$user->name = 'Jane Doe';
197
198
$dao->save($user);
198
199
199
- // Deleting a user
200
+ // Delete a user
200
201
$dao->delete($user);
201
202
// or
202
203
$dao->delete($user->uid);
@@ -276,7 +277,7 @@ class Users extends DAO {
276
277
}
277
278
278
279
public function findByDepartment($department, $order = NULL) {
279
- return $this->find(array('department' => $email ), $order);
280
+ return $this->find(array('department' => $department ), $order);
280
281
}
281
282
}
282
283
```
@@ -286,7 +287,7 @@ the new Query API. Please read the [Query API](#query-api) chapter.
286
287
287
288
## Using Data Objects with DAOs
288
289
289
- As above mentioned, you can use your own data classes. There are actually no
290
+ As mentioned above , you can use your own data classes. There are actually no
290
291
restrictions other than the class needs a no-argument constructor. The main
291
292
advantage is that this class can have additional methods that have some
292
293
logic. You can even define additional attributes that will not be saved in
@@ -332,7 +333,7 @@ $products = $model->get('products')->find();
332
333
Of course, a better idea is to encapsulate this in your own ` DataModel ` subclass:
333
334
334
335
```
335
- class MyDataModel extends \TGDatabase \DataModel {
336
+ class MyDataModel extends \TgDatabase \DataModel {
336
337
337
338
public function __construct($database) {
338
339
parent::__construct($database);
@@ -630,14 +631,14 @@ $count = $dao->count(Restrictions::eq('name', 'John Doe'));
630
631
```
631
632
632
633
## Advantages and Limitations
633
- The Query API will further ease searching objects in a database and return model classes, using more
634
- complex expressions and restrictions. You will be able to dynamically apply restrictions depending on
635
- the requirements of your front-end users and your application. And you won 't need the DAO once you
634
+ The Query API further eases searching objects in a database and return model classes, using more
635
+ complex expressions and restrictions. You are able to dynamically apply restrictions depending on
636
+ the requirements of your front-end users and your application. And you don 't need the DAO once you
636
637
created the ` Query ` object. It is self-contained.
637
638
638
639
However, some limitations exist:
639
640
640
- * Query API supports basic use cases so far (searching objects with basic restrictions).
641
+ * Query API supports basic use cases so far (searching objects with basic restrictions, updates, deleting ).
641
642
* Only MySQL / MariaDB SQL dialect is produced (but can be extended to other dialects easily when you stick to the API).
642
643
* A few of the limitations may be ovecome by using the ` SqlExpression ` and ` SqlProjection ` classes:
643
644
@@ -646,7 +647,7 @@ However, some limitations exist:
646
647
$myExpr = Restrictions::sql('my-sql-fragment');
647
648
648
649
// Use a specific projection not supported
649
- $myProj = Projections::sql('name, email ');
650
+ $myProj = Projections::sql('RANDOM() ');
650
651
```
651
652
652
653
But feel free to raise an issue (see below) when you need some extension that is not yet supported.
0 commit comments