Proposed extension to support ON DUPLICATE KEY UPDATE clause

Issue #25 resolved
Daniel DiLauro created an issue

Please consider adding a function set similar to the following to allow ON DUPLICATE KEY UPDATE clauses:

*In Arrays section

    var $array_odku = array(); //ON DUPLICATE KEY UPDATE

*In reset function

        $this -> array_odku = array();  //ON DUPLICATE KEY UPDATE

*Among function definitions

    /**
     * Sets the ON DUPLICATE KEY UPDATE clause
     * Must be called before ->insert()
     * Multiple instances are joined by comma ", " during insert prep
     * @param $key array of column names. Can either be string or array.
     * Can be callled with:
     * array: $dups = array('city', 'state', 'zip'); ->odku($dups)
     *     or ->odku(array('city', 'state', 'zip'))
     * or with single column names / chaining: ->odku('city')->odku('state')->odku('zip')
     */

    public function odku($key)
    {
        return $this -> _odku($key);
    }


    protected function _odku($key)
    {
        /**
         * If the ON DUPLICATE KEY UPDATE key is an array then we process the array
         */

        if (is_array($key))
        {
            foreach ($key as $key => $val)
            {
                $this -> _odku($val);
            }
        }
        else
        {
            if ($this -> isReservedWord($key) == true)
                $this -> array_odku[] = "`$key` = VALUES($key)";
            else
                $this -> array_odku[] = "$key  = VALUES($key)";
        }
        return $this;

    }

*within "insert" function, just before $this -> execute(); , add:

        if (count($this -> array_odku) > 0)
        {
            $this -> _query .= " ON DUPLICATE KEY UPDATE ";
            $this -> _query .= implode(", ", $this -> array_odku);
        }

Thank you for the work you've already done on this class - very impressive!

Comments (1)

  1. Log in to comment