public function testSql_InsertDuplicateKey() { $Sql_Insert = new A_Sql_Insert(); $Sql_Insert->table('foobar')->values(array('foo' => 'barbaz'))->updateIfDuplicateKey('foo'); $query = $Sql_Insert->render(); $this->assertEqual($query, 'INSERT INTO foobar (foo) VALUES (\'barbaz\') ON DUPLICATE KEY UPDATE `foo` = VALUES(`foo`)'); }
public function save($data = array()) { if ($data) { $this->_data = $data; } if (!$this->is_loaded) { $insert = new A_Sql_Insert(); $insert->table($this->table)->values($this->_data); $this->sql = $insert->render(); $this->db->query($this->sql); $try_update = !$this->db->isError(); } if (isset($this->_data[$this->key]) && ($this->is_loaded || $try_update)) { $update = new A_Sql_Update(); $update->table($this->table)->set($this->_data)->where($this->key, $this->_data[$this->key]); $this->sql = $update->render(); $this->db->query($this->sql); } return $this; }
<?php include 'config.php'; $values = array(array('foo' => 'Boo', 'bar' => 'Ca\'r', 'baz' => 'Caz'), array('goo' => 'Goo', 'gar' => 'Ga\'r', 'gaz' => 'Gaz')); $insert = new A_Sql_Insert(); $insert->table('mytable')->values($values); echo "A_Sql_Insert::render=" . $insert->render() . '<br/>'; dump($insert);