Skip to content

Commit b59cfb9

Browse files
committed
Added Documentation
1 parent 39608eb commit b59cfb9

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

system/Database/BaseBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2657,7 +2657,7 @@ public function delete($where = '', ?int $limit = null, bool $resetData = true)
26572657
* Sets data and calls batchExecute to run queries
26582658
*
26592659
* @param array|object|null $set a dataset or select query
2660-
* @param array|RawSql|string|null $constraints
2660+
* @param array|RawSql|null $constraints
26612661
*
26622662
* @return false|int|string[] Number of rows affected or FALSE on failure, SQL array when testMode
26632663
*/

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Query Builder
159159
- Improved the SQL structure for ``Builder::updateBatch()``. See :ref:`update-batch` for the details.
160160
- Added ``when()`` and ``whenNot()`` methods to conditionally add clauses to the query. See :ref:`BaseBuilder::when() <db-builder-when>` for details.
161161
- Added ``upsert()`` and ``upsertBatch()`` methods to QueryBuilder. See :ref:`upsert-data`.
162+
- Added ``deleteBatch()`` methods to QueryBuilder. See :ref:`delete-batch`.
162163

163164
Forge
164165
-----

user_guide_src/source/database/query_builder.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,17 @@ the data to the first parameter of the method:
11071107
If you want to delete all data from a table, you can use the ``truncate()``
11081108
method, or ``emptyTable()``.
11091109

1110+
.. _delete-batch:
1111+
1112+
$builder->deleteBatch()
1113+
------------------
1114+
1115+
Generates a batch **DELETE** statement based on a set of data.
1116+
1117+
.. literalinclude:: query_builder/118.php
1118+
1119+
This method may be especially useful when deleting data in a table with a composite primary key.
1120+
11101121
$builder->emptyTable()
11111122
----------------------
11121123

@@ -1882,6 +1893,16 @@ Class Reference
18821893

18831894
Compiles and executes a ``DELETE`` query.
18841895

1896+
.. php:method:: deleteBatch([$set = null[, $constraints = null[, $batchSize = 100]]])
1897+
1898+
:param array|object|null $set: Field name, or an associative array of field/value pairs
1899+
:param array|RawSql|string|null $constraints: The field or fields used as keys to delete on.
1900+
:param int $batchSize: Count of conditions to group in a single query
1901+
:returns: Number of rows deleted or ``false`` on failure
1902+
:rtype: int|false
1903+
1904+
Compiles and executes batch ``DELETE`` query.
1905+
18851906
.. php:method:: increment($column[, $value = 1])
18861907
18871908
:param string $column: The name of the column to increment
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
$data = [
4+
[
5+
'order' => 48372,
6+
'line' => 3,
7+
'product' => 'Keyboard',
8+
'qty' => 1,
9+
],
10+
[
11+
'order' => 48372,
12+
'line' => 4,
13+
'product' => 'Mouse',
14+
'qty' => 1,
15+
],
16+
[
17+
'order' => 48372,
18+
'line' => 5,
19+
'product' => 'Monitor',
20+
'qty' => 2,
21+
],
22+
];
23+
24+
$builder->setAlias('del')->where('order_line.qty >', 1)->deleteBatch($data, 'order, line');
25+
26+
// OR
27+
$builder
28+
->setData($data, true, 'del')
29+
->onConstraint('order, line')
30+
->where('order_line.qty >', 1)
31+
->deleteBatch();
32+
33+
/*
34+
* MySQL Produces:
35+
* DELETE `order_line` FROM `order_line`
36+
* INNER JOIN (
37+
* SELECT 48372 `order`, 3 `line`, 'Keyboard' `product`, 1 `qty` UNION ALL
38+
* SELECT 48372 `order`, 4 `line`, 'Mouse' `product`, 1 `qty` UNION ALL
39+
* SELECT 48372 `order`, 5 `line`, 'Monitor' `product`, 2 `qty`
40+
* ) `del`
41+
* ON `order_line`.`order` = `del`.`order` AND `order_line`.`line` = `del`.`line`
42+
* WHERE `order_line`.`qty` > 1
43+
*/

0 commit comments

Comments
 (0)