Skip to content
This repository was archived by the owner on Nov 26, 2022. It is now read-only.

Commit b8c63ba

Browse files
author
Daniel Opitz
committed
Updated docs
1 parent d987e98 commit b8c63ba

File tree

5 files changed

+897
-900
lines changed

5 files changed

+897
-900
lines changed

docs/deletes.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
## Deletes
2+
3+
Create a delete object:
4+
5+
```php
6+
use Odan\Database\Connection;
7+
8+
$db = new Connection($dsn, $username, $password, $options);
9+
$delete = $db->delete();
10+
```
11+
12+
The query builder may also be used to delete records from the
13+
table via the delete method. You may constrain delete
14+
statements by adding where clauses before calling the delete method:
15+
16+
17+
```php
18+
$db->delete()->from('users')->execute(); // DELETE FROM `users`
19+
$db->delete()->from('users')->where('votes', '>', 100)->execute(); // DELETE FROM `users` WHERE `votes` > '100'
20+
```
21+
22+
If you wish to truncate the entire table, which will remove
23+
all rows and reset the auto-incrementing ID to zero,
24+
you may use the truncate method:
25+
26+
```php
27+
$db->delete()->from('users')->truncate()->execute(); // TRUNCATE TABLE `users`;
28+
```
29+
30+
### Order of Deletion
31+
32+
If the DELETE statement includes an ORDER BY clause, rows are deleted in the
33+
order specified by the clause. This is useful primarily in conjunction with LIMIT.
34+
35+
```php
36+
$db->delete()->from('some_logs')
37+
->where('username', '=', 'jcole')
38+
->orderBy('created_at')
39+
->limit(1)
40+
->execute();
41+
```
42+
43+
ORDER BY also helps to delete rows in an order required to avoid referential integrity violations.
44+
45+
### Delete Limit
46+
47+
The LIMIT clause places a limit on the number of rows that can be deleted.
48+
49+
```php
50+
$db->delete()->from('users')->limit(10)->execute();
51+
```
52+
53+
### Delete Low Priority
54+
55+
If you specify `LOW_PRIORITY`, the server delays execution of the
56+
DELETE until no other clients are reading from the table.
57+
58+
This affects only storage engines that use only table-level
59+
locking (such as MyISAM, MEMORY, and MERGE).
60+
61+
```php
62+
$db->delete()->from('users')->lowPriority()->execute();
63+
```
64+
65+
### Delete and ignore errors
66+
67+
The `IGNORE` modifier causes MySQL to ignore errors during the process of deleting rows.
68+
69+
(Errors encountered during the parsing stage are processed in the usual manner.)
70+
71+
Errors that are ignored due to the use of IGNORE are returned as warnings.
72+
73+
```php
74+
$db->delete()->from('users')->ignore()->execute();
75+
```
76+
77+
### Delete Quick modifier
78+
79+
For MyISAM tables, if you use the QUICK modifier, the storage engine
80+
does not merge index leaves during delete, which may speed up some kinds of delete operations.
81+
82+
```php
83+
$db->delete()->from('users')->quick()->execute();
84+
```

docs/inserts.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Inserts
2+
3+
Create a insert object:
4+
5+
```php
6+
use Odan\Database\Connection;
7+
8+
$db = new Connection($dsn, $username, $password, $options);
9+
$insert = $db->insert();
10+
```
11+
12+
### Insert A Single Row
13+
14+
The query builder also provides an insert method for inserting
15+
records into the database table.
16+
17+
The insert method accepts an array of column names and values:
18+
19+
```php
20+
$db->insert()
21+
->into('test')
22+
->set(['email' => '[email protected]', 'votes' => 0])
23+
->execute();
24+
```
25+
26+
You may even insert several records into the table with a single call
27+
to insert by passing an array of arrays. Each array represents a
28+
row to be inserted into the table:
29+
30+
```php
31+
$db->insert()
32+
->into('test')->set([
33+
['email' => '[email protected]', 'votes' => 0],
34+
['email' => '[email protected]', 'votes' => 0]
35+
])->execute();
36+
```
37+
38+
### Auto-Incrementing IDs
39+
40+
If the table has an auto-incrementing id,
41+
use the insertGetId method to insert a record and then retrieve the ID:
42+
43+
```php
44+
$userId = $db->insert()->into('users')->insertGetId(['email' => '[email protected]', 'votes' => 0]);
45+
```
46+
47+
Another way to get the last inserted ID:
48+
49+
```php
50+
$db->insert()
51+
->into('users')
52+
->set(['email' => '[email protected]', 'votes' => 0])
53+
->execute();
54+
55+
$userId = $db->lastInsertId();
56+
```
57+
58+
### Number of rows affected by the last statement
59+
60+
Sometimes you need more then just the last inserted ID, for example the number of affected rows.
61+
You can find this information in the Statement object:
62+
63+
```php
64+
$stmt = $db->insert()
65+
->into('users')
66+
->set(['email' => '[email protected]', 'votes' => 0])
67+
->prepare();
68+
69+
$stmt->execute();
70+
$rowCount = $stmt->rowCount(); // 1
71+
```

0 commit comments

Comments
 (0)