@@ -876,6 +876,90 @@ The first parameter is an associative array of values.
876
876
877
877
.. warning :: When you use ``RawSql``, you MUST escape the data manually. Failure to do so could result in SQL injections.
878
878
879
+ .. _upsert-data :
880
+
881
+ **************
882
+ Upserting Data
883
+ **************
884
+
885
+ Upsert
886
+ ======
887
+
888
+ $builder->upsert()
889
+ ------------------
890
+
891
+ Generates an upsert string based on the data you supply, and runs the
892
+ query. You can either pass an **array ** or an **object ** to the
893
+ method. By default a constraint will be defined in order. A primary
894
+ key will be selected first and then unique keys. MySQL will use any
895
+ constraint by default. Here is an example using an array:
896
+
897
+ .. literalinclude :: query_builder/112.php
898
+
899
+ The first parameter is an associative array of values.
900
+
901
+ Here is an example using an object:
902
+
903
+ .. literalinclude :: query_builder/113.php
904
+
905
+ The first parameter is an object.
906
+
907
+ .. note :: All values are escaped automatically producing safer queries.
908
+
909
+ $builder->getCompiledUpsert()
910
+ -----------------------------
911
+
912
+ Compiles the upsert query just like ``$builder->upsert() `` but does not
913
+ *run * the query. This method simply returns the SQL query as a string.
914
+
915
+ Example:
916
+
917
+ .. literalinclude :: query_builder/114.php
918
+
919
+ .. note :: This method doesn't work for batch upserts.
920
+
921
+ upsertBatch
922
+ ===========
923
+
924
+ $builder->upsertBatch()
925
+ -----------------------
926
+
927
+ Generates an upsert string based on the data you supply, and runs the
928
+ query. You can either pass an **array ** or an **object ** to the
929
+ method. By default a constraint will be defined in order. A primary
930
+ key will be selected first and then unique keys. Mysql will use any
931
+ constraint by default. Here is an example using an array:
932
+
933
+ .. literalinclude :: query_builder/108.php
934
+
935
+ The first parameter is an associative array of values.
936
+
937
+ .. note :: All values are escaped automatically producing safer queries.
938
+
939
+ $builder->onConstraint()
940
+ ------------------------
941
+
942
+ Allows manually setting constraint to be used for upsert. This does
943
+ not work with MySQL because MySQL checks all constraints by default.
944
+
945
+ .. literalinclude :: query_builder/109.php
946
+
947
+ This method accepts a string or an array of columns.
948
+
949
+ $builder->updateFields()
950
+ ------------------------
951
+ Allows manually setting the fields to be updated when performing upserts.
952
+
953
+ .. literalinclude :: query_builder/110.php
954
+
955
+ This method accepts a string, an array of columns, or RawSql. You can also
956
+ specify an extra column to be updated that isn't included in the dataset.
957
+ This can be done by setting the second parameter to ``true ``.
958
+
959
+ .. literalinclude :: query_builder/111.php
960
+
961
+ Notice that the ``updated_at `` field is not inserted but is used on update.
962
+
879
963
*************
880
964
Updating Data
881
965
*************
@@ -1667,6 +1751,31 @@ Class Reference
1667
1751
1668
1752
.. important :: This method is deprecated. It will be removed in future releases.
1669
1753
1754
+ .. php :method :: upsert([$set = null[, $escape = null]])
1755
+ :param array $set: An associative array of field/value pairs
1756
+ :param bool $escape: Whether to escape values
1757
+ :returns: ``true `` on success, ``false `` on failure
1758
+ :rtype: bool
1759
+
1760
+ Compiles and executes an ``UPSERT `` statement.
1761
+
1762
+ .. php :method :: upsertBatch([$set = null[, $escape = null[, $batch_size = 100]]])
1763
+ :param array $set: Data to upsert
1764
+ :param bool $escape: Whether to escape values
1765
+ :param int $batch_size: Count of rows to upsert at once
1766
+ :returns: Number of rows upserted or ``false `` on failure
1767
+ :rtype: int|false
1768
+
1769
+ Compiles and executes batch ``UPSERT `` statements.
1770
+
1771
+ .. note :: MySQL uses ``ON DUPLICATE KEY UPDATE``, the affected-rows value
1772
+ per row is 1 if the row is inserted as a new row, 2 if an existing row
1773
+ is updated, and 0 if an existing row is set to its current values.
1774
+
1775
+ .. note :: When more than ``$batch_size`` rows are provided, multiple
1776
+ ``UPSERT `` queries will be executed, each trying to upsert
1777
+ up to ``$batch_size `` rows.
1778
+
1670
1779
.. php :method :: update([$set = null[, $where = null[, $limit = null]]])
1671
1780
1672
1781
:param array $set: An associative array of field/value pairs
0 commit comments