attributeLabels()); }*/ public function init() { } public function insertUpdate($runValidation = true, $attributes = null) { if ($runValidation && !$this->validate($attributes)) { Yii::info('Model not inserted due to validation error.', __METHOD__); return false; } if (!$this->isTransactional(self::OP_INSERT)) { return $this->insertUpdateInternal($attributes); } $transaction = static::getDb()->beginTransaction(); try { $result = $this->insertUpdateInternal($attributes); if ($result === false) { $transaction->rollBack(); } else { $transaction->commit(); } return $result; } catch (\Exception $e) { $transaction->rollBack(); throw $e; } catch (\Throwable $e) { $transaction->rollBack(); throw $e; } } protected function insertUpdateInternal($attributes = null) { if (!$this->beforeSave(true)) { return false; } $values = $this->getDirtyAttributes($attributes); if (($primaryKeys = $this->schemaInsertUpdate(static::tableName(), $values)) === false) { return false; } foreach ($primaryKeys as $name => $value) { $id = static::getTableSchema()->columns[$name]->phpTypecast($value); $this->setAttribute($name, $id); $values[$name] = $id; } $changedAttributes = array_fill_keys(array_keys($values), null); $this->setOldAttributes($values); $this->afterSave(true, $changedAttributes); return true; } public function schemaInsertUpdate($table, $columns) { $db = static::getDb(); foreach ($this->attributes() as $itemColumn) { $column = $db->getSchema()->quoteColumnName($itemColumn); $onDuplicateKeyValues[] = $column . ' = VALUES(' . $column . ')'; } $command = $db->createCommand()->insert($table, $columns); $sql = $command->getRawSql(); $sql .= ' ON DUPLICATE KEY UPDATE ' . implode(', ', $onDuplicateKeyValues);; if (!$db->createCommand($sql)->execute()) { return false; } $tableSchema = $this->getTableSchema($table); $result = []; foreach ($tableSchema->primaryKey as $name) { if ($tableSchema->columns[$name]->autoIncrement) { $result[$name] = $this->getLastInsertID($tableSchema->sequenceName); break; } $result[$name] = isset($columns[$name]) ? $columns[$name] : $tableSchema->columns[$name]->defaultValue; } return $result; } }