Skip to content

Commit 79102eb

Browse files
committed
Update User Guide
1 parent 5befd31 commit 79102eb

File tree

4 files changed

+76
-20
lines changed

4 files changed

+76
-20
lines changed

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Others
4545
- ``RouteCollection::resetRoutes()`` resets Auto-Discovery of Routes. Previously once discovered, RouteCollection never discover Routes files again even if ``RouteCollection::resetRoutes()`` is called.
4646
- ``CITestStreamFilter::$buffer = ''`` no longer causes the filter to be registered to listen for streams. Now there
4747
is a ``CITestStreamFilter::registration()`` method for this. See :ref:`upgrade-430-stream-filter` for details.
48+
- The second parameter ``$index`` of ``BaseBuilder::updateBatch()`` has changed to ``$constraints``. It now accepts an array, string, or ``RawSql``.
4849

4950
Method Signature Changes
5051
========================

user_guide_src/source/database/query_builder.rst

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,19 +1608,45 @@ Class Reference
16081608

16091609
Compiles and executes an ``UPDATE`` statement.
16101610

1611-
.. php:method:: updateBatch([$set = null[, $value = null[, $batch_size = 100]]])
1611+
.. php:method:: updateBatch([$set = null[, $constraints = null[, $batchSize = 100]]])
16121612
16131613
:param array $set: Field name, or an associative array of field/value pairs
1614-
:param string $value: Field value, if $set is a single field
1615-
:param int $batch_size: Count of conditions to group in a single query
1614+
:param mixed $constraints: The field or fields used as keys to update on.
1615+
:param int $batchSize: Count of conditions to group in a single query
16161616
:returns: Number of rows updated or ``false`` on failure
16171617
:rtype: int|false
16181618

16191619
Compiles and executes batch ``UPDATE`` statements.
16201620

1621-
.. note:: When more than ``$batch_size`` field/value pairs are provided,
1622-
multiple queries will be executed, each handling up to
1623-
``$batch_size`` field/value pairs.
1621+
.. note:: Set ``$batchSize`` to 0 for unlimited. ``$constraints`` takes a comma delimited string of columns, and array, associative array, or RawSql.
1622+
1623+
.. php:method:: updateFields($set, [$addToDefault = false, [$ignore = null]])
1624+
1625+
:param mixed $set: Row of columns or array of rows, a row is an array or object
1626+
:param bool $addToDefault: Adds an additional column than those in dataset
1627+
:param bool $ignore: An array of columns to ignore from those in $set
1628+
:returns: ``BaseBuilder`` instance (method chaining)
1629+
:rtype: ``BaseBuilder``
1630+
1631+
Used with ``updateBatch()`` and ``upsertBatch()`` methods. This defines the fields which will be updated.
1632+
1633+
.. php:method:: onConstraint($set)
1634+
1635+
:param mixed $set: A set of fields or field used has keys or constraints
1636+
:returns: ``BaseBuilder`` instance (method chaining)
1637+
:rtype: ``BaseBuilder``
1638+
1639+
Used with ``updateBatch()`` and ``upsertBatch()`` methods. This takes a comma delimited string of columns, and array, associative array, or RawSql.
1640+
1641+
.. php:method:: setData($set, [$escape = null, [$alias = '']])
1642+
1643+
:param mixed $set: Row of columns or array of rows, a row is an array or object
1644+
:param bool $escape: Whether to escape values
1645+
:param bool $alias: A table alias for dataset
1646+
:returns: ``BaseBuilder`` instance (method chaining)
1647+
:rtype: ``BaseBuilder``
1648+
1649+
Used for ``*Batch()`` methods to set data for insert, update, upsert.
16241650

16251651
.. php:method:: setUpdateBatch($key[, $value = ''[, $escape = null]])
16261652
Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,57 @@
11
<?php
22

3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
312
$data = [
413
[
5-
'title' => 'Title 1',
6-
'name' => 'Name 1',
7-
'date' => 'Date 1',
14+
'title' => 'Title 1',
15+
'author' => 'Author 1',
16+
'name' => 'Name 1',
17+
'date' => 'Date 1',
818
],
919
[
10-
'title' => 'Title 2',
11-
'name' => 'Name 2',
12-
'date' => 'Date 2',
20+
'title' => 'Title 2',
21+
'author' => 'Author 2',
22+
'name' => 'Name 2',
23+
'date' => 'Date 2',
1324
],
1425
];
1526

16-
$builder->updateBatch($data, 'title');
27+
$builder->updateBatch($data, ['title', 'author']);
28+
29+
// OR
30+
$builder->setData($data)->onConstraint('title, author')->updateBatch();
31+
32+
// OR
33+
$builder->setData($data)
34+
->setAlias('u')
35+
->onConstraint(['`mytable`.`title`' => '`u`.`title`', 'author' => new RawSql('`u`.`author`')])
36+
->updateBatch();
37+
38+
// OR
39+
$builder->setData($data)
40+
->setAlias('u')
41+
->onConstraint(new RawSql('`mytable`.`title` = `u`.`title` AND `mytable`.`author` = `u`.`author`'))
42+
->updateFields(['last_update' => new RawSql('CURRENT_TIMESTAMP()')], true)
43+
->updateBatch();
1744
/*
1845
* Produces:
1946
* UPDATE `mytable`
2047
* INNER JOIN (
21-
* SELECT 'Title 1' `title`, 'Name 1' `name`, 'Date 1' `date` UNION ALL
22-
* SELECT 'Title 2' `title`, 'Name 2' `name`, 'Date 2' `date`
23-
* ) u
24-
* ON `mytable`.`title` = u.`title`
48+
* SELECT 'Title 1' `title`, 'Author 1' `author`, 'Name 1' `name`, 'Date 1' `date` UNION ALL
49+
* SELECT 'Title 2' `title`, 'Author 2' `author`, 'Name 2' `name`, 'Date 2' `date`
50+
* ) `u`
51+
* ON `mytable`.`title` = `u`.`title` AND `mytable`.`author` = `u`.`author`
2552
* SET
26-
* `mytable`.`title` = u.`title`,
27-
* `mytable`.`name` = u.`name`,
28-
* `mytable`.`date` = u.`date`
53+
* `mytable`.`title` = `u`.`title`,
54+
* `mytable`.`name` = `u`.`name`,
55+
* `mytable`.`date` = `u`.`date`,
56+
* `mytable`.`last_update` = CURRENT_TIMESTAMP() // this only applies to the last scenario
2957
*/

user_guide_src/source/installation/upgrade_430.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Breaking Enhancements
128128
- Since the launch of Spark Commands was extracted from ``CodeIgniter\CodeIgniter``, there may be problems running these commands if the ``Services::codeigniter()`` service has been overridden.
129129
- The return type of ``CodeIgniter\Database\Database::loadForge()`` has been changed to ``Forge``. Extending classes should likewise change the type.
130130
- The return type of ``CodeIgniter\Database\Database::loadUtils()`` has been changed to ``BaseUtils``. Extending classes should likewise change the type.
131+
- The second parameter ``$index`` of ``BaseBuilder::updateBatch()`` has changed to ``$constraints``. It now accepts types array, string, or ``RawSql``. Extending classes should likewise change types.
131132

132133
Project Files
133134
*************

0 commit comments

Comments
 (0)