Skip to content

Commit bac0b0b

Browse files
committed
Add documentation and change log
1 parent 8f09056 commit bac0b0b

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
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
@@ -141,6 +141,7 @@ Database
141141
- Added the ability to manually set index names. These methods include: ``Forge::addKey()``, ``Forge::addPrimaryKey()``, and ``Forge::addUniqueKey()``
142142
- Fixed ``Forge::dropKey()`` to allow droping unique indexes. This required the ``DROP CONSTRAINT`` SQL command.
143143
- Added ``upsert()`` and ``upsertBatch()`` methods to QueryBuilder. See :ref:`upsert-data`.
144+
- Added ``BaseBuilder::fromQuery()`` which allows insert, update, upsert from a query. See :ref:`insert-batch-data`.
144145

145146
Model
146147
=====

user_guide_src/source/database/query_builder.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,8 @@ The reason the second query worked is that the first parameter is set to ``false
858858

859859
.. note:: This method doesn't work for batch inserts.
860860

861+
.. _insert-batch-data:
862+
861863
insertBatch
862864
===========
863865

@@ -876,6 +878,10 @@ The first parameter is an associative array of values.
876878

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

881+
You can also insert from a query:
882+
883+
.. literalinclude:: query_builder/117.php
884+
879885
.. _upsert-data:
880886

881887
**************
@@ -936,6 +942,10 @@ The first parameter is an associative array of values.
936942

937943
.. note:: All values are escaped automatically producing safer queries.
938944

945+
You can also upsert from a query:
946+
947+
.. literalinclude:: query_builder/115.php
948+
939949
$builder->onConstraint()
940950
------------------------
941951

@@ -1071,6 +1081,10 @@ The first parameter is an associative array of values, the second parameter is t
10711081
.. note:: ``affectedRows()`` won't give you proper results with this method,
10721082
due to the very nature of how it works. Instead, ``updateBatch()``
10731083
returns the number of rows affected.
1084+
1085+
You can also update from a query:
1086+
1087+
.. literalinclude:: query_builder/116.php
10741088

10751089
$builder->getCompiledUpdate()
10761090
-----------------------------
@@ -1358,6 +1372,14 @@ Class Reference
13581372

13591373
Specifies the ``FROM`` clause of a query using a subquery.
13601374

1375+
.. php:method:: fromQuery($query)
1376+
1377+
:param mixed $query: Instance of the BaseBuilder or RawSql class or string
1378+
:returns: ``BaseBuilder`` instance (method chaining)
1379+
:rtype: ``BaseBuilder``
1380+
1381+
Sets a query as a datasource for ``insertBatch()``, ``updateBatch()``, ``upsertBatch()``.
1382+
13611383
.. php:method:: join($table, $cond[, $type = ''[, $escape = null]])
13621384
13631385
:param string $table: Table name to join
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
$query = $this->db->table('user2')
4+
->select('user2.name, user2.email, user2.country')
5+
->join('user', 'user.email = user2.email', 'left')
6+
->where('user.email IS NULL');
7+
8+
$additionalUpdateField = ['updated_at' => new RawSql('CURRENT_TIMESTAMP')];
9+
10+
$sql = $builder->onConstraint('email')->updateFields($additionalUpdateField, true)->upsertBatch($query);
11+
/* MySQLi produces:
12+
INSERT INTO `db_user` (`country`, `email`, `name`)
13+
SELECT user2.name, user2.email, user2.country
14+
FROM user2
15+
LEFT JOIN user ON user.email = user2.email
16+
WHERE user.email IS NULL
17+
ON DUPLICATE KEY UPDATE
18+
`country` = VALUES(`country`),
19+
`email` = VALUES(`email`),
20+
`name` = VALUES(`name`),
21+
`updated_at` = CURRENT_TIMESTAMP
22+
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
$query = $this->db->table('user2')
4+
->select('user2.name, user2.email, user2.country')
5+
->join('user', 'user.email = user2.email', 'inner')
6+
->where('user2.country', 'US');
7+
8+
$additionalUpdateField = ['updated_at' => new RawSql('CURRENT_TIMESTAMP')];
9+
10+
$sql = $builder->table('user')
11+
->setAlias('u')
12+
->onConstraint('email')
13+
->updateFields($additionalUpdateField, true)
14+
->updateBatch($query);
15+
/*
16+
* Produces:
17+
* UPDATE `user`
18+
* INNER JOIN (
19+
* SELECT user2.name, user2.email, user2.country
20+
* FROM user2
21+
* INNER JOIN user ON user.email = user2.email
22+
* WHERE user2.country = 'US'
23+
* ) `u`
24+
* ON `user`.`email` = `u`.`email`
25+
* SET
26+
* `mytable`.`name` = `u`.`name`,
27+
* `mytable`.`email` = `u`.`email`,
28+
* `mytable`.`country` = `u`.`country`,
29+
* `mytable`.`updated_at` = CURRENT_TIMESTAMP()
30+
*/
31+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$query = "SELECT user2.name, user2.email, user2.country
4+
FROM user2
5+
LEFT JOIN user ON user.email = user2.email
6+
WHERE user.email IS NULL";
7+
8+
$sql = $builder->ignore(true)->insertBatch($query);
9+
/* MySQLi produces:
10+
INSERT IGNORE INTO `db_user` (`name`, `country`, `email`)
11+
SELECT user2.name, user2.email, user2.country
12+
FROM user2
13+
LEFT JOIN user ON user.email = user2.email
14+
WHERE user.email IS NULL
15+
*/

0 commit comments

Comments
 (0)