Skip to content

Commit b5f2531

Browse files
committed
- Changed README.md to document more possibilities of the builder.
1 parent 0c4c4e3 commit b5f2531

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

README.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ Here a few things to keep in mind:
2323
```PHP
2424
$pdo = new PDO('mysql:host=127.0.0.1;dbname=test;charset=utf8', 'root', '');
2525
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
26+
```
2627

28+
```PHP
2729
$mysql = new MySQL($pdo);
2830
$mysql->getAliasRegistry()->add('textprefix', 'testdb.test__');
2931
```
@@ -40,32 +42,57 @@ $select = $mysql->select(['customer_count' => 'COUNT(*)'])
4042
->orderBy('t1.field2', 'DESC')
4143
->limit(100)
4244
->offset(50);
45+
```
4346

47+
```PHP
4448
if($contition === true) {
4549
$select->where('t1.somefield = ?', $someValue);
4650
}
51+
```
4752

53+
```PHP
4854
$rows = $select->fetchRows();
49-
5055
foreach($rows as $row) {
5156
print_r($row);
5257
}
5358
```
5459

55-
### Insert
60+
* The order of method-calls doesn't matter.
5661

57-
You can insert key-value-arrays with `addAll`, `updateAll`, `addOrUpdateAll`. As the second parameter you can provide an array to specify the only fields to consider.
62+
### Insert
5863

64+
You can insert key-value-arrays with `addAll`, `updateAll`, `addOrUpdateAll`. As the second parameter you can provide an array to specify the only fields to consider.
65+
5966
```PHP
60-
$mysql->insert()
67+
$id = $mysql->insert()
6168
->into('test')
6269
->addOrUpdateAll($data, ['field1', 'field2', 'field3'])
63-
->add('created_at=NOW()')
70+
->add('created_by', $userId)
71+
->addOrUpdate('updated_by', $userId)
72+
->addExpr('created_at=NOW()')
6473
->addOrUpdateExpr('updated_at=NOW()')
6574
->run();
6675
```
6776

68-
There is also an option to build an `INSERT INTO ... SELECT ... FROM ... ON DUPLICATE KEY UPDATE ...`
77+
* `insert()` alwasy returns an id, no matter if a dataset was actually inserted or updated.
78+
* You can mass-insert by using `insert()->...->insertRows(array $rows)`.
79+
80+
There is also an option to build an `INSERT INTO ... SELECT ... FROM ... ON DUPLICATE KEY UPDATE ...`:
81+
82+
```PHP
83+
$id = $mysql->insert()
84+
->into('test')
85+
->addExpr('field1=:field1')
86+
->addOrUpdateExpr('field2=:field2')
87+
->addExpr('field3=NOW()')
88+
->from(
89+
$mysql->select()
90+
->field('a.myfield1', 'field1')
91+
->field('a.myfield2', 'field2')
92+
->from('a', 'mytable')
93+
->where('field=?', 1)
94+
)->run();
95+
```
6996

7097
### Update
7198

0 commit comments

Comments
 (0)