Is there a way to do an “INSERT…ON DUPLICATE KEY UPDATE” in Zend Framework 1.5?

后端 未结 7 1392
独厮守ぢ
独厮守ぢ 2021-01-31 02:25

I would like to use ON DUPLICATE KEY UPDATE in Zend Framework 1.5, is this possible?

Example

INSERT INTO sometable (...)
VALUES (...)
ON DUP         


        
7条回答
  •  粉色の甜心
    2021-01-31 02:47

    I worked for Zend and specifically worked on Zend_Db quite a bit.

    No, there is no API support for the ON DUPLICATE KEY UPDATE syntax. For this case, you must simply use query() and form the complete SQL statement yourself.

    I do not recommend interpolating values into the SQL as harvejs shows. Use query parameters.

    Edit: You can avoid repeating the parameters by using VALUES() expressions.

    $sql = "INSERT INTO sometable (id, col2, col3) VALUES (:id, :col2, :col3)
      ON DUPLICATE KEY UPDATE col2 = VALUES(col2), col3 = VALUES(col3)";
    
    $values = array("id"=>1, "col2"=>327, "col3"=>"active");
    

提交回复
热议问题