This commit is contained in:
2021-09-28 18:54:07 +08:00
parent 27e87a461e
commit 079cd12399
18 changed files with 659 additions and 678 deletions
+1 -3
View File
@@ -167,9 +167,7 @@ trait Builder
* @param $condition
* @param $array
* @return string
* @throws NotFindClassException
* @throws ReflectionException
* @throws ReflectionException
* @throws Exception
*/
private function _arrayMap($condition, $array): string
{
+10 -11
View File
@@ -9,9 +9,8 @@ declare(strict_types=1);
namespace Database\Traits;
use Database\ActiveRecord;
use Database\ModelInterface;
use Database\Collection;
use Database\IOrm;
use Database\Relation;
use Exception;
@@ -24,11 +23,11 @@ use Exception;
abstract class HasBase implements \Database\Traits\Relation
{
/** @var ActiveRecord|Collection */
protected Collection|ActiveRecord $data;
/** @var ModelInterface|Collection */
protected Collection|ModelInterface $data;
/**
* @var IOrm|ActiveRecord
* @var ModelInterface
*/
protected mixed $model;
@@ -40,7 +39,7 @@ abstract class HasBase implements \Database\Traits\Relation
/**
* HasBase constructor.
* @param IOrm $model
* @param ModelInterface $model
* @param $primaryId
* @param $value
* @param Relation $relation
@@ -49,16 +48,16 @@ abstract class HasBase implements \Database\Traits\Relation
public function __construct(mixed $model, $primaryId, $value, Relation $relation)
{
if (!class_exists($model)) {
throw new Exception('Model must implement ' . ActiveRecord::class);
throw new Exception('Model must implement ' . $model);
}
if (!in_array(IOrm::class, class_implements($model))) {
throw new Exception('Model must implement ' . ActiveRecord::class);
if (!in_array(ModelInterface::class, class_implements($model))) {
throw new Exception('Model must implement ' . $model);
}
if (is_array($value)) {
if (empty($value)) $value = [];
$_model = $model::find()->whereIn($primaryId, $value);
$_model = $model::query()->whereIn($primaryId, $value);
} else {
$_model = $model::find()->where(['t1.' . $primaryId => $value]);
$_model = $model::query()->where(['t1.' . $primaryId => $value]);
}
$this->_relation = $relation->bindIdentification($model, $_model);
+17 -14
View File
@@ -12,8 +12,8 @@ namespace Database\Traits;
use Closure;
use Database\ActiveQuery;
use Database\ActiveRecord;
use Database\Condition\MathematicsCondition;
use Database\ModelInterface;
use Database\Query;
use Database\SqlBuilder;
use Exception;
@@ -44,9 +44,9 @@ trait QueryTrait
private SqlBuilder $builder;
/**
* @var ActiveRecord|string|null
* @var ModelInterface|string|null
*/
public ActiveRecord|string|null $modelClass;
public ModelInterface|string|null $modelClass;
/**
* clear
@@ -140,7 +140,7 @@ trait QueryTrait
*/
public function getTable(): string
{
return $this->modelClass::getTable();
return $this->modelClass->getTable();
}
@@ -306,10 +306,11 @@ trait QueryTrait
public function leftJoin(string $tableName, string $alias, $onCondition, $param = NULL): static
{
if (class_exists($tableName)) {
if (!in_array(ActiveRecord::class, class_implements($tableName))) {
throw new Exception('Model must implement ' . $tableName);
$model = Kiri::getDi()->get($tableName);
if (!($model instanceof ModelInterface)) {
throw new Exception('Model must implement ' . ModelInterface::class);
}
$tableName = $tableName::getTable();
$tableName = $model->getTable();
}
return $this->join(...["LEFT JOIN " . $tableName, $alias, $onCondition, $param]);
}
@@ -325,10 +326,11 @@ trait QueryTrait
public function rightJoin($tableName, $alias, $onCondition, $param = NULL): static
{
if (class_exists($tableName)) {
if (!in_array(ActiveRecord::class, class_implements($tableName))) {
throw new Exception('Model must implement ' . $tableName);
$model = Kiri::getDi()->get($tableName);
if (!($model instanceof ModelInterface)) {
throw new Exception('Model must implement ' . ModelInterface::class);
}
$tableName = $tableName::getTable();
$tableName = $model->getTable();
}
return $this->join(...["RIGHT JOIN " . $tableName, $alias, $onCondition, $param]);
}
@@ -344,10 +346,11 @@ trait QueryTrait
public function innerJoin($tableName, $alias, $onCondition, $param = NULL): static
{
if (class_exists($tableName)) {
if (!in_array(ActiveRecord::class, class_implements($tableName))) {
throw new Exception('Model must implement ' . $tableName);
$model = Kiri::getDi()->get($tableName);
if (!($model instanceof ModelInterface)) {
throw new Exception('Model must implement ' . ModelInterface::class);
}
$tableName = $tableName::getTable();
$tableName = $model->getTable();
}
return $this->join(...["INNER JOIN " . $tableName, $alias, $onCondition, $param]);
}
@@ -727,7 +730,7 @@ trait QueryTrait
$activeQuery = new ActiveQuery($this->modelClass);
call_user_func($value, $activeQuery);
if (empty($activeQuery->from)) {
$activeQuery->from($activeQuery->modelClass::getTable());
$activeQuery->from($activeQuery->modelClass->getTable());
}
return $activeQuery;
}