Skip to content

Commit 9c381f1

Browse files
authored
Merge pull request #7223 from kenjis/docs-improve-query_builder
docs: improve query_builder.rst
2 parents 135f81d + 09276f8 commit 9c381f1

4 files changed

Lines changed: 75 additions & 27 deletions

File tree

user_guide_src/source/database/query_builder.rst

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,8 @@ Upsert
908908
$builder->upsert()
909909
------------------
910910

911+
.. versionadded:: 4.3.0
912+
911913
Generates an upsert string based on the data you supply, and runs the
912914
query. You can either pass an **array** or an **object** to the
913915
method. By default a constraint will be defined in order. A primary
@@ -929,6 +931,8 @@ The first parameter is an object.
929931
$builder->getCompiledUpsert()
930932
-----------------------------
931933

934+
.. versionadded:: 4.3.0
935+
932936
Compiles the upsert query just like ``$builder->upsert()`` but does not
933937
*run* the query. This method simply returns the SQL query as a string.
934938

@@ -944,10 +948,12 @@ upsertBatch
944948
$builder->upsertBatch()
945949
-----------------------
946950

951+
.. versionadded:: 4.3.0
952+
947953
Generates an upsert string based on the data you supply, and runs the
948954
query. You can either pass an **array** or an **object** to the
949955
method. By default a constraint will be defined in order. A primary
950-
key will be selected first and then unique keys. Mysql will use any
956+
key will be selected first and then unique keys. MySQL will use any
951957
constraint by default. Here is an example using an array:
952958

953959
.. literalinclude:: query_builder/108.php
@@ -960,13 +966,16 @@ You can also upsert from a query:
960966

961967
.. literalinclude:: query_builder/115.php
962968

963-
.. note:: ``setQueryAsData()`` can be used since v4.3.0.
969+
.. note:: The ``setQueryAsData()``, ``onConstraint()``, and ``updateFields()``
970+
methods can be used since v4.3.0.
964971

965972
.. note:: It is required to alias the columns of the select query to match those of the target table.
966973

967974
$builder->onConstraint()
968975
------------------------
969976

977+
.. versionadded:: 4.3.0
978+
970979
Allows manually setting constraint to be used for upsert. This does
971980
not work with MySQL because MySQL checks all constraints by default.
972981

@@ -976,6 +985,9 @@ This method accepts a string or an array of columns.
976985

977986
$builder->updateFields()
978987
------------------------
988+
989+
.. versionadded:: 4.3.0
990+
979991
Allows manually setting the fields to be updated when performing upserts.
980992

981993
.. literalinclude:: query_builder/110.php
@@ -1084,14 +1096,24 @@ UpdateBatch
10841096
$builder->updateBatch()
10851097
-----------------------
10861098

1099+
.. note:: Since v4.3.0, the second parameter ``$index`` of ``updateBatch()`` has
1100+
changed to ``$constraints``. It now accepts types array, string, or ``RawSql``.
1101+
10871102
Generates an update string based on the data you supply, and runs the query.
10881103
You can either pass an **array** or an **object** to the method.
10891104
Here is an example using an array:
10901105

10911106
.. literalinclude:: query_builder/092.php
10921107

1108+
.. note:: Since v4.3.0, the generated SQL structure has been Improved.
1109+
10931110
The first parameter is an associative array of values, the second parameter is the where key.
10941111

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

10971119
.. warning:: When you use ``RawSql``, you MUST escape the data manually. Failure to do so could result in SQL injections.
@@ -1104,7 +1126,8 @@ You can also update from a query:
11041126

11051127
.. literalinclude:: query_builder/116.php
11061128

1107-
.. note:: ``setQueryAsData()`` can be used since v4.3.0.
1129+
.. note:: The ``setQueryAsData()``, ``onConstraint()``, and ``updateFields()``
1130+
methods can be used since v4.3.0.
11081131

11091132
.. note:: It is required to alias the columns of the select query to match those of the target table.
11101133

@@ -1146,6 +1169,8 @@ method, or ``emptyTable()``.
11461169
$builder->deleteBatch()
11471170
-----------------------
11481171

1172+
.. versionadded:: 4.3.0
1173+
11491174
Generates a batch **DELETE** statement based on a set of data.
11501175

11511176
.. literalinclude:: query_builder/118.php
@@ -1198,6 +1223,8 @@ When
11981223
$builder->when()
11991224
----------------
12001225

1226+
.. versionadded:: 4.3.0
1227+
12011228
This allows modifying the query based on a condition without breaking out of the
12021229
query builder chain. The first parameter is the condition, and it should evaluate
12031230
to a boolean. The second parameter is a callable that will be ran
@@ -1223,6 +1250,8 @@ WhenNot
12231250
$builder->whenNot()
12241251
-------------------
12251252

1253+
.. versionadded:: 4.3.0
1254+
12261255
This works exactly the same way as ``$builder->when()`` except that it will
12271256
only run the callable when the condition evaluates to ``false``, instead of ``true`` like ``when()``.
12281257

@@ -1415,6 +1444,8 @@ Class Reference
14151444

14161445
.. php:method:: setQueryAsData($query[, $alias[, $columns = null]])
14171446
1447+
.. versionadded:: 4.3.0
1448+
14181449
:param BaseBuilder|RawSql $query: Instance of the BaseBuilder or RawSql
14191450
:param string|null $alias: Alias for query
14201451
:param array|string|null $columns: Array or comma delimited string of columns in the query

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
*/

user_guide_src/source/database/query_builder/115.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
$additionalUpdateField = ['updated_at' => new RawSql('CURRENT_TIMESTAMP')];
99

10-
$sql = $builder->setQueryAsData($query)->onConstraint('email')->updateFields($additionalUpdateField, true)->upsertBatch();
10+
$sql = $builder->setQueryAsData($query)
11+
->onConstraint('email')
12+
->updateFields($additionalUpdateField, true)
13+
->upsertBatch();
1114
/* MySQLi produces:
1215
INSERT INTO `db_user` (`country`, `email`, `name`)
1316
SELECT user2.name, user2.email, user2.country
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)