本文整理汇总了PHP中DBAdapter::executeSQL方法的典型用法代码示例。如果您正苦于以下问题:PHP DBAdapter::executeSQL方法的具体用法?PHP DBAdapter::executeSQL怎么用?PHP DBAdapter::executeSQL使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DBAdapter
的用法示例。
在下文中一共展示了DBAdapter::executeSQL方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: save
public function save()
{
$prepared = array();
// prepare values
foreach ($this->_data as $field => $value) {
$fieldType = self::$_metadata[$field];
$prepared[$field] = $this->_prepareValue($fieldType, $value);
}
// do the save (insert/update)
if ($this->_isNew) {
// insert
$fields = array();
$values = array();
foreach ($prepared as $field => $preparedValue) {
$fields[] = $field;
$values[] = $preparedValue;
}
$fields = implode(", ", $fields);
$values = implode(", ", $values);
// prepare sql
$sql = "INSERT INTO {$this->_table} ({$fields}) VALUES ({$values}) ";
// execute
$res = $this->_db->executeSQL($sql);
if (!$res) {
throw new ModelException("Invalid query - " . $this->_db->getError());
}
// update the pointer to primary key
$this->_id = $this->_data[self::$_primaryField];
if (is_null($this->_id)) {
$this->_id = $this->lastInsertId();
}
} else {
// update
$updateSet = array();
foreach ($prepared as $field => $preparedValue) {
$updateSet[] = "{$field} = {$preparedValue}";
}
$updateSet = implode(", ", $updateSet);
// prepare primary key
$prim = self::$_primaryField;
$preparedId = $this->_prepareValue(self::$_metadata[$prim], $this->_id);
//prepare sql
$sql = "UPDATE `{$this->_table}` SET {$updateSet} WHERE `{$prim}` = {$preparedId}";
// execute
$res = $this->_db->executeSQL($sql);
if (!$res) {
throw new ModelException("Invalid query - " . $this->_db->getError());
}
// update the pointer to primary key
$this->_id = $this->_data[self::$_primaryField];
}
// re-load the object
$this->load($this->_id);
}