public function flush() { if (!$this->buffer) { return FALSE; } $this->lastFlush = time(); $rows = Query::insert($this->table)->batchSaveRows($this->buffer)->exec(); $this->buffer = array(); return $rows; }
/** * 通过关联数据插入某表一行数据 * * @param array $setArray 插入数组 */ public function insert($setArray) { $insert = Query::insert(static::TABLE); if ($this->dbAdapter !== NULL) { $insert->setAdapter($this->dbAdapter); } return $insert->kvInsert($setArray)->query(); }
/** * 获取Query更新对象 * * @throws Exception * @return Query */ protected function getSaveQuery() { $tableInstance = $this->tableInstance; if ($tableInstance === NULL || $tableInstance::TABLE === NULL) { throw new Exception('Table class constant TABLE has not been set.'); } $table = $tableInstance::TABLE; if ($this->newRow) { //新行 // PRIMAY_IDKEY 可以设置为默认值0,自动增长的也会自动更新;不然有些非自动增长的会有问题 $insert = Query::insert($table); if ($this->dbAdapter !== NULL) { $insert->setAdapter($this->dbAdapter); } if ($this->duplicateCreate) { $insert->enableDuplicate(); //dup的时候要注意去除主键为0的情况 $duplicate = $this->data; unset($duplicate[static::PRIMAY_IDKEY]); $insert->dupFields(array_keys($duplicate)); } return $insert->kvInsert($this->data); } else { if (!$this->editData) { //无修改,直接返回成功 return FALSE; } $primaryValue = $this->oldPrimaryIdValue === NULL ? $this->data[static::PRIMAY_IDKEY] : $this->oldPrimaryIdValue; $update = Query::update($table); if ($this->dbAdapter !== NULL) { $update->setAdapter($this->dbAdapter); } return $update->sets($this->editData)->where(Where::createPair(static::PRIMAY_IDKEY, $primaryValue))->limit(1); } }