@@ -851,6 +851,82 @@ The first parameter is an associative array of values.
851
851
852
852
.. note :: All values are escaped automatically producing safer queries.
853
853
854
+ **************
855
+ Upserting Data
856
+ **************
857
+
858
+ Upsert
859
+ ======
860
+
861
+ $builder->upsert()
862
+ ------------------
863
+
864
+ Generates an upsert string based on the data you supply, and runs the
865
+ query. You can either pass an **array ** or an **object ** to the
866
+ method. By default a constraint will be defined in order. A primary
867
+ key will be selected first and then unique keys. Mysql will use any
868
+ constraint by default. Here is an example using an array:
869
+
870
+ .. literalinclude :: query_builder/105.php
871
+
872
+ The first parameter is an associative array of values.
873
+
874
+ Here is an example using an object:
875
+
876
+ .. literalinclude :: query_builder/106.php
877
+
878
+ The first parameter is an object.
879
+
880
+ .. note :: All values are escaped automatically producing safer queries.
881
+
882
+ $builder->getCompiledUpsert()
883
+ -----------------------------
884
+
885
+ Compiles the upsert query just like ``$builder->upsert() `` but does not
886
+ *run * the query. This method simply returns the SQL query as a string.
887
+
888
+ Example:
889
+
890
+ .. literalinclude :: query_builder/107.php
891
+
892
+ .. note :: This method doesn't work for batch upserts.
893
+
894
+ upsertBatch
895
+ ===========
896
+
897
+ $builder->upsertBatch()
898
+ -----------------------
899
+
900
+ Generates an upsert string based on the data you supply, and runs the
901
+ query. You can either pass an **array ** or an **object ** to the
902
+ method. By default a constraint will be defined in order. A primary
903
+ key will be selected first and then unique keys. Mysql will use any
904
+ constraint by default. Here is an example using an array:
905
+
906
+ .. literalinclude :: query_builder/108.php
907
+
908
+ The first parameter is an associative array of values.
909
+
910
+ .. note :: All values are escaped automatically producing safer queries.
911
+
912
+ $builder->onConstraint()
913
+ ------------------------
914
+
915
+ Allows manually setting constraint to be used for upsert. This does
916
+ not work with MySQL because MySQL checks all constraints by default.
917
+
918
+ .. literalinclude :: query_builder/109.php
919
+
920
+ This method accepts a string or an array of columns.
921
+
922
+ $builder->updateFields()
923
+ ------------------------
924
+ Allows manually setting the fields to be updated when performing upserts.
925
+
926
+ .. literalinclude :: query_builder/110.php
927
+
928
+ This method accepts a string or an array of columns.
929
+
854
930
*************
855
931
Updating Data
856
932
*************
@@ -1555,7 +1631,7 @@ Class Reference
1555
1631
:returns: ``BaseBuilder `` instance (method chaining)
1556
1632
:rtype: ``BaseBuilder ``
1557
1633
1558
- Adds field/value pairs to be passed later to ``insert() ``, ``update() `` or ``replace() ``.
1634
+ Adds field/value pairs to be passed later to ``insert() ``, ``upsert() ``, `` update() `` or ``replace() ``.
1559
1635
1560
1636
.. php :method :: insert([$set = null[, $escape = null]])
1561
1637
@@ -1590,6 +1666,59 @@ Class Reference
1590
1666
1591
1667
Adds field/value pairs to be inserted in a table later via ``insertBatch() ``.
1592
1668
1669
+ .. php :method :: upsert([$set = null[, $escape = null]])
1670
+
1671
+ :param array $set: An associative array of field/value pairs
1672
+ :param bool $escape: Whether to escape values
1673
+ :returns: ``true `` on success, ``false `` on failure
1674
+ :rtype: bool
1675
+
1676
+ Compiles and executes an ``UPSERT `` statement.
1677
+
1678
+ .. php :method :: upsertBatch([$set = null[, $escape = null[, $batch_size = 100]]])
1679
+
1680
+ :param array $set: Data to upsert
1681
+ :param bool $escape: Whether to escape values
1682
+ :param int $batch_size: Count of rows to upsert at once
1683
+ :returns: Number of rows upserted or ``false `` on failure
1684
+ :rtype: int|false
1685
+
1686
+ Compiles and executes batch ``UPSERT `` statements.
1687
+
1688
+ .. note :: MySQL uses ``ON DUPLICATE KEY UPDATE``, the affected-rows value
1689
+ per row is 1 if the row is inserted as a new row, 2 if an existing row
1690
+ is updated, and 0 if an existing row is set to its current values.
1691
+
1692
+ .. note :: When more than ``$batch_size`` rows are provided, multiple
1693
+ ``UPSERT `` queries will be executed, each trying to upsert
1694
+ up to ``$batch_size `` rows.
1695
+
1696
+ .. php :method :: setBatch($key[, $value = ''[, $escape = null]])
1697
+
1698
+ :param mixed $key: Field name or an array of field/value pairs
1699
+ :param string $value: Field value, if $key is a single field
1700
+ :param bool $escape: Whether to escape values
1701
+ :returns: ``BaseBuilder `` instance (method chaining)
1702
+ :rtype: ``BaseBuilder ``
1703
+
1704
+ Adds field/value pairs to be upserted in a table later via ``upsertBatch() ``.
1705
+
1706
+ .. php :method :: onConstraint($keys)
1707
+
1708
+ :param array|string $keys: List of columns used as constraints
1709
+ :returns: ``BaseBuilder `` instance (method chaining)
1710
+ :rtype: ``BaseBuilder ``
1711
+
1712
+ Defines fields used as constraints for ``upsert() `` and ``upsertBatch() ``.
1713
+
1714
+ .. php :method :: updateFields($keys)
1715
+
1716
+ :param array|string $keys: List of fields to be updated
1717
+ :returns: ``BaseBuilder `` instance (method chaining)
1718
+ :rtype: ``BaseBuilder ``
1719
+
1720
+ Defines fields to be updated for ``upsert() `` and ``upsertBatch() ``.
1721
+
1593
1722
.. php :method :: update([$set = null[, $where = null[, $limit = null]]])
1594
1723
1595
1724
:param array $set: An associative array of field/value pairs
@@ -1693,6 +1822,13 @@ Class Reference
1693
1822
1694
1823
Compiles an ``INSERT `` statement and returns it as a string.
1695
1824
1825
+ .. php :method :: getCompiledUpsert()
1826
+
1827
+ :returns: The compiled SQL statement as a string
1828
+ :rtype: string
1829
+
1830
+ Compiles an ``UPSERT `` statement and returns it as a string.
1831
+
1696
1832
.. php :method :: getCompiledUpdate([$reset = true])
1697
1833
1698
1834
:param bool $reset: Whether to reset the current QB values or not
0 commit comments