@@ -112,6 +112,18 @@ class MysqliDb
112112 */
113113 protected $ isSubQuery = false ;
114114
115+ /**
116+ * Name of the auto increment column
117+ *
118+ */
119+ protected $ _lastInsertId = null ;
120+
121+ /**
122+ * Column names for update when using onDuplicate method
123+ *
124+ */
125+ protected $ _updateColumns = null ;
126+
115127 /**
116128 * Return type: 'Array' to return results as array, 'Object' as object
117129 * 'Json' as json string
@@ -235,6 +247,8 @@ protected function reset()
235247 $ this ->returnType = 'Array ' ;
236248 $ this ->_nestJoin = false ;
237249 $ this ->_tableName = '' ;
250+ $ this ->_lastInsertId = null ;
251+ $ this ->_updateColumns = null ;
238252 }
239253
240254 /**
@@ -549,6 +563,19 @@ public function where($whereProp, $whereValue = 'DBNULL', $operator = '=', $cond
549563 return $ this ;
550564 }
551565
566+ /**
567+ * This function store update column's name and column name of the
568+ * autoincrement column
569+ *
570+ * @param Array Variable with values
571+ * @param String Variable value
572+ */
573+ public function onDuplicate ($ _updateColumns , $ _lastInsertId = null )
574+ {
575+ $ this ->_lastInsertId = $ _lastInsertId ;
576+ $ this ->_updateColumns = $ _updateColumns ;
577+ }
578+
552579 /**
553580 * This method allows you to specify multiple (method chaining optional) OR WHERE statements for SQL queries.
554581 *
@@ -778,6 +805,57 @@ private function _buildInsert ($tableName, $insertData, $operation)
778805 return true ;
779806 }
780807
808+ /**
809+ * Helper function to add variables into the query statement
810+ *
811+ * @param Array Variable with values
812+ */
813+ protected function _buildDuplicate ($ tableData )
814+ {
815+ if (is_array ($ this ->_updateColumns ) && !empty ($ this ->_updateColumns )) {
816+ $ this ->_query .= " on duplicate key update " ;
817+ if ($ this ->_lastInsertId ) {
818+ $ this ->_lastQuery .= $ this ->_lastInsertId ."=LAST_INSERT_ID( " .$ this ->_lastInsertId ."), " ;
819+ $ this ->_lastInsertId = null ;
820+ }
821+
822+ foreach ($ this ->_updateColumns as $ column ) {
823+ $ this ->_query .= "` " . $ column . "` = " ;
824+
825+ // Simple value
826+ if (!is_array ($ tableData [$ column ])) {
827+ $ this ->_bindParam ($ tableData [$ column ]);
828+ $ this ->_query .= '?, ' ;
829+ continue ;
830+ }
831+
832+ // Function value
833+ $ arr = $ tableData [$ column ];
834+ $ key = key ($ arr );
835+ $ val = $ arr [$ key ];
836+ switch ($ key ) {
837+ case '[I] ' :
838+ $ this ->_query .= $ column . $ val . ", " ;
839+ break ;
840+ case '[F] ' :
841+ $ this ->_query .= $ val [0 ] . ", " ;
842+ if (!empty ($ val [1 ]))
843+ $ this ->_bindParams ($ val [1 ]);
844+ break ;
845+ case '[N] ' :
846+ if ($ val == null )
847+ $ this ->_query .= "! " . $ column . ", " ;
848+ else
849+ $ this ->_query .= "! " . $ val . ", " ;
850+ break ;
851+ default :
852+ die ("Wrong operation " );
853+ }
854+ }
855+ $ this ->_query = rtrim ($ this ->_query , ', ' );
856+ }
857+ }
858+
781859 /**
782860 * Abstraction method that will compile the WHERE statement,
783861 * any passed update data, and the desired rows.
@@ -797,6 +875,7 @@ protected function _buildQuery($numRows = null, $tableData = null)
797875 $ this ->_buildGroupBy ();
798876 $ this ->_buildOrderBy ();
799877 $ this ->_buildLimit ($ numRows );
878+ $ this ->_buildDuplicate ($ tableData );
800879
801880 $ this ->_lastQuery = $ this ->replacePlaceHolders ($ this ->_query , $ this ->_bindParams );
802881
0 commit comments