This commit is contained in:
2021-08-02 16:38:50 +08:00
parent fd7415b7f1
commit a095c94e21
15 changed files with 1593 additions and 1513 deletions
+4 -19
View File
@@ -12,7 +12,6 @@ namespace Database;
use Database\Traits\QueryTrait;
use Exception;
use Snowflake\Abstracts\Component;
use Snowflake\Snowflake;
/**
* Class ActiveQuery
@@ -107,22 +106,12 @@ class ActiveQuery extends Component implements ISqlBuilder
if (is_string($name)) {
$name = explode(',', $name);
}
foreach ($name as $key => $val) {
foreach ($name as $val) {
array_push($this->with, $val);
}
return $this;
}
/**
* @param bool $isArray
* @return $this
*/
public function asArray(bool $isArray = TRUE): static
{
$this->asArray = $isArray;
return $this;
}
/**
* @param $sql
@@ -137,20 +126,16 @@ class ActiveQuery extends Component implements ISqlBuilder
/**
* @return ActiveRecord|array|null
* @return ActiveRecord|null
* @throws Exception
*/
public function first(): ActiveRecord|array|null
public function first(): ActiveRecord|null
{
$data = $this->execute($this->builder->one())->one();
if (empty($data)) {
return NULL;
}
$newModel = $this->modelClass::populate($data);
if ($this->asArray) {
return $newModel->toArray();
}
return $newModel;
return $this->modelClass::populate($data);
}
+22 -6
View File
@@ -43,7 +43,7 @@ use validator\Validator;
* @property bool $isCreate
* @method rules()
* @method static tableName()
* @property Application $application
* @property Application $container
*/
abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
{
@@ -106,7 +106,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
/**
* @return Application
*/
#[Pure] private function getApplication(): Application
#[Pure] private function getContainer(): Application
{
return Snowflake::app();
}
@@ -322,14 +322,30 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
/**
* @return ActiveQuery
* @throws ReflectionException
* @throws NotFindClassException
*/
public static function find(): ActiveQuery
{
return Snowflake::createObject(ActiveQuery::class, [static::class]);
return new ActiveQuery(get_called_class());
}
/**
* @return ActiveQuery
*/
public static function query(): ActiveQuery
{
return new ActiveQuery(get_called_class());
}
/**
* @throws ConfigException
*/
protected function getConnection()
{
return Config::get('connections.' . static::$ab_name, null, true);
}
/**
* @param null $condition
* @param array $attributes
@@ -710,7 +726,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
* @return bool
* @throws Exception
*/
#[Event(ActiveRecord::AFTER_SAVE)]
#[Event(self::AFTER_SAVE)]
public function afterSave($attributes, $changeAttributes): bool
{
return true;