This commit is contained in:
2021-08-03 11:44:58 +08:00
parent 3f9b8d8e8c
commit 72b8da4c7f
5 changed files with 115 additions and 105 deletions
+10 -8
View File
@@ -90,6 +90,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
protected array $actions = [];
/** @var string */
private static string $connection = 'db';
/**
* @var Relation|null
*/
@@ -325,7 +329,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/
public static function find(): ActiveQuery
{
return new ActiveQuery(get_called_class());
return static::query();
}
/**
@@ -342,7 +346,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/
protected function getConnection()
{
return Config::get('connections.' . static::$ab_name, null, true);
return Config::get('connections.' . static::$connection, null, true);
}
@@ -445,7 +449,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
private function insert($param, $attributes): bool|static
{
[$sql, $param] = SqlBuilder::builder(static::find())->insert($param);
$dbConnection = static::getDb()->createCommand($sql, static::getDbName(), $param);
$dbConnection = static::getDb()->createCommand($sql, $param);
if (!($lastId = (int)$dbConnection->save(true, $this))) {
throw new Exception('保存失败.');
}
@@ -498,7 +502,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
if (is_bool($generate)) {
return $generate;
}
$command = static::getDb()->createCommand($generate[0], static::getDbName(), $generate[1]);
$command = static::getDb()->createCommand($generate[0], $generate[1]);
if ($command->save(false, $this)) {
return $this->refresh()->afterSave($fields, $param);
}
@@ -995,9 +999,7 @@ 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 Snowflake::app()->db->get(static::$connection = $dbName);
}
@@ -1007,7 +1009,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/
public static function getDbName(): string
{
return Config::get('databases.connections.' . static::$ab_name . '.database');
return Config::get('databases.connections.' . static::$connection . '.database');
}