/** * 插入数据 * * @param string $table 表名 * @param array $data 要插入的数据 * @param bool $replace 是否替换插入 * @param bool $multi 是否批量插入 * @param bool $ignore 是否忽略错误 * @return string|array 最后插入的ID,批量插入的话返回所有ID */ public function insert($table, $data, $replace = false, $multi = false, $ignore = false) { if (!$multi) { $data = array($data); } $st = microtime(true); $fields = '`' . implode('`,`', array_keys($data[0])) . '`'; //字段 $values = '(' . str_repeat('?,', count($data[0]) - 1) . '?)'; $method = $replace ? 'REPLACE ' : 'INSERT'; $ignore = !$replace && $ignore ? 'IGNORE' : ''; $sql = $this->sql("{$method} {$ignore} INTO {$table} ({$fields}) VALUES {$values}"); $conn = $this->getConnect(); $stm = $conn->prepare($sql); $ids = array(); foreach ($data as $row) { $stm->execute(array_values($row)); $ids[] = $conn->lastInsertId(); } if ($this->debug && $this->logger) { $this->logger->info("sql:{$sql}, data:" . json_encode($data) . ", time:" . round(microtime(true) - $st, 4)); } return $multi ? $ids : array_shift($ids); }
public function initLogger($name) { $config = App::conf('app', 'logger', array()); $logger = new Logger($name); $logger->setTimeZone(new \DateTimeZone('PRC')); if (isset($config[$name])) { foreach ($config[$name] as $conf) { $class = '\\Core\\Logger\\Handler\\' . $conf['handler']; $logger->setHandler(new $class($conf['config']), $conf['level']); } } return $logger; }