Skip to content

Commit cd4cc57

Browse files
committed
docs: split sample code and add explanation
1 parent ec20625 commit cd4cc57

3 files changed

Lines changed: 42 additions & 23 deletions

File tree

user_guide_src/source/database/query_builder.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,11 @@ Here is an example using an array:
11091109

11101110
The first parameter is an associative array of values, the second parameter is the where key.
11111111

1112+
Since v4.3.0, you can also use the ``setQueryAsData()``, ``onConstraint()``, and
1113+
``updateFields()`` methods:
1114+
1115+
.. literalinclude:: query_builder/120.php
1116+
11121117
.. note:: All values except ``RawSql`` are escaped automatically producing safer queries.
11131118

11141119
.. warning:: When you use ``RawSql``, you MUST escape the data manually. Failure to do so could result in SQL injections.

user_guide_src/source/database/query_builder/092.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,7 @@
1414
'date' => 'Date 2',
1515
],
1616
];
17-
1817
$builder->updateBatch($data, ['title', 'author']);
19-
20-
// OR
21-
$builder->setData($data)->onConstraint('title, author')->updateBatch();
22-
23-
// OR
24-
$builder->setData($data, null, 'u')
25-
->onConstraint(['`mytable`.`title`' => '`u`.`title`', 'author' => new RawSql('`u`.`author`')])
26-
->updateBatch();
27-
28-
// OR
29-
foreach ($data as $row) {
30-
$builder->setData($row);
31-
}
32-
$builder->onConstraint('title, author')->updateBatch();
33-
34-
// OR
35-
$builder->setData($data, true, 'u')
36-
->onConstraint(new RawSql('`mytable`.`title` = `u`.`title` AND `mytable`.`author` = `u`.`author`'))
37-
->updateFields(['last_update' => new RawSql('CURRENT_TIMESTAMP()')], true)
38-
->updateBatch();
3918
/*
4019
* Produces:
4120
* UPDATE `mytable`
@@ -47,6 +26,5 @@
4726
* SET
4827
* `mytable`.`title` = `u`.`title`,
4928
* `mytable`.`name` = `u`.`name`,
50-
* `mytable`.`date` = `u`.`date`,
51-
* `mytable`.`last_update` = CURRENT_TIMESTAMP() // this only applies to the last scenario
29+
* `mytable`.`date` = `u`.`date`
5230
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use CodeIgniter\Database\RawSql;
4+
5+
$builder->setData($data)->onConstraint('title, author')->updateBatch();
6+
7+
// OR
8+
$builder->setData($data, null, 'u')
9+
->onConstraint(['`mytable`.`title`' => '`u`.`title`', 'author' => new RawSql('`u`.`author`')])
10+
->updateBatch();
11+
12+
// OR
13+
foreach ($data as $row) {
14+
$builder->setData($row);
15+
}
16+
$builder->onConstraint('title, author')->updateBatch();
17+
18+
// OR
19+
$builder->setData($data, true, 'u')
20+
->onConstraint(new RawSql('`mytable`.`title` = `u`.`title` AND `mytable`.`author` = `u`.`author`'))
21+
->updateFields(['last_update' => new RawSql('CURRENT_TIMESTAMP()')], true)
22+
->updateBatch();
23+
/*
24+
* Produces:
25+
* UPDATE `mytable`
26+
* INNER JOIN (
27+
* SELECT 'Title 1' `title`, 'Author 1' `author`, 'Name 1' `name`, 'Date 1' `date` UNION ALL
28+
* SELECT 'Title 2' `title`, 'Author 2' `author`, 'Name 2' `name`, 'Date 2' `date`
29+
* ) `u`
30+
* ON `mytable`.`title` = `u`.`title` AND `mytable`.`author` = `u`.`author`
31+
* SET
32+
* `mytable`.`title` = `u`.`title`,
33+
* `mytable`.`name` = `u`.`name`,
34+
* `mytable`.`date` = `u`.`date`,
35+
* `mytable`.`last_update` = CURRENT_TIMESTAMP() // this only applies to the last scenario
36+
*/

0 commit comments

Comments
 (0)