diff --git a/Database/ActiveQuery.php b/Database/ActiveQuery.php index 60b12d3e..ead6c5ea 100644 --- a/Database/ActiveQuery.php +++ b/Database/ActiveQuery.php @@ -132,7 +132,7 @@ class ActiveQuery extends Component implements ISqlBuilder */ public function execute($sql, $params = []): Command { - return $this->modelClass::getDb()->createCommand($sql, $params); + return $this->modelClass::getDb()->createCommand($sql, $this->modelClass::getDbName(), $params); } diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index d1c02538..4334a221 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -196,7 +196,7 @@ class ActiveRecord extends BaseActiveRecord if (is_bool($create)) { return false; } - return static::getDb()->createCommand($create[0], $create[1])->exec(); + return static::getDb()->createCommand($create[0], static::getDbName(), $create[1])->exec(); } diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index 59fd3b3a..8121814a 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -433,7 +433,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess // $trance = $dbConnection->beginTransaction(); try { - if (!($lastId = (int)$dbConnection->createCommand($sql, $param)->save(true, $this))) { + if (!($lastId = (int)$dbConnection->createCommand($sql, static::getDbName(), $param)->save(true, $this))) { throw new Exception('保存失败.'); } // $trance->commit(); @@ -499,7 +499,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess } // $trance = $command->beginTransaction(); - if (!$command->createCommand(...$generate)->save(false, $this)) { + if (!$command->createCommand($generate[0], static::getDbName(), $generate[1])->save(false, $this)) { // $trance->rollback(); return false; } @@ -769,6 +769,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess return true; } + + private static string $ab_name = ''; + + /** * @return Connection * @throws Exception @@ -998,7 +1002,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess public function offsetUnset(mixed $offset) { if (!isset($this->_attributes[$offset]) - && !isset($this->_oldAttributes[$offset])) { + && !isset($this->_oldAttributes[$offset])) { return; } unset($this->_attributes[$offset]); @@ -1032,9 +1036,21 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public static function setDatabaseConnect($dbName): Connection { + static::$ab_name = $dbName; + return Snowflake::app()->db->get($dbName); } + + /** + * @return string + */ + public static function getDbName() + { + return static::$ab_name; + } + + /** * @return Columns * @throws Exception @@ -1043,7 +1059,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess { return static::getDb()->getSchema() ->getColumns() - ->table(static::getTable()); + ->table(static::getDbName() . '.' . static::getTable()); } /** diff --git a/Database/Connection.php b/Database/Connection.php index d18f2bc1..5b6d41c9 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -263,14 +263,14 @@ class Connection extends Component * @return Command * @throws */ - public function createCommand($sql = null, array $attributes = []): Command + public function createCommand($sql = null, $dbname = '', array $attributes = []): Command { $substr = strtoupper(substr($sql, 0, 6)); $sql = match ($substr) { - 'SELECT', 'SHOW F', 'DELETE' => preg_replace('/FROM\s+(\w+)\s+/', 'FROM `' . $this->database . '`.$1 ', $sql), - 'UPDATE' => preg_replace('/UPDATE\s+(\w+)\s+SET/', 'UPDATE `' . $this->database . '`.$1 SET', $sql), - 'INSERT' => preg_replace('/INSERT INTO\s+(\w+)\s+/', 'INSERT INTO `' . $this->database . '`.$1', $sql), - 'TRUNCA' => preg_replace('/TRUNCATE\s+(\w+)\s+/', 'TRUNCATE `' . $this->database . '`.$1', $sql), + 'SELECT', 'SHOW F', 'DELETE' => preg_replace('/FROM\s+(\w+)\s+/', 'FROM `' . $dbname . '`.$1 ', $sql), + 'UPDATE' => preg_replace('/UPDATE\s+(\w+)\s+SET/', 'UPDATE `' . $dbname . '`.$1 SET', $sql), + 'INSERT' => preg_replace('/INSERT INTO\s+(\w+)\s+/', 'INSERT INTO `' . $dbname . '`.$1', $sql), + 'TRUNCA' => preg_replace('/TRUNCATE\s+(\w+)\s+/', 'TRUNCATE `' . $dbname . '`.$1', $sql), default => throw new Exception('database error') }; $command = new Command(['db' => $this, 'sql' => $sql]); diff --git a/System/Di/Service.php b/System/Di/Service.php index 4e96977f..fab64ba4 100644 --- a/System/Di/Service.php +++ b/System/Di/Service.php @@ -62,11 +62,6 @@ class Service extends Component $object = Snowflake::createObject($config); } - - unset($config['class']); - - Snowflake::configure($object, $config); - return $this->_components[$id] = $object; }