Files
kiri-core/Database/HasBase.php
T

79 lines
1.3 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 15:47
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 12:38:32 +08:00
namespace Database;
use Exception;
/**
* Class HasBase
* @package Database
*
* @include Query
*/
abstract class HasBase
{
/** @var ActiveRecord|Collection */
protected $data;
2020-10-29 18:17:25 +08:00
/**
* @var string
*/
protected string $model;
2020-08-31 12:38:32 +08:00
/** @var */
protected $primaryId;
/** @var array */
2020-10-29 18:17:25 +08:00
protected array $value = [];
2020-08-31 12:38:32 +08:00
/** @var Relation $_relation */
2020-10-29 18:17:25 +08:00
protected Relation $_relation;
2020-08-31 12:38:32 +08:00
/**
* HasBase constructor.
2020-10-29 18:17:25 +08:00
* @param $model
2020-08-31 12:38:32 +08:00
* @param $primaryId
* @param $value
* @param Relation $relation
* @throws Exception
*/
2020-10-29 18:17:25 +08:00
public function __construct($model, $primaryId, $value, Relation $relation)
2020-08-31 12:38:32 +08:00
{
if (is_array($value)) {
if (empty($value)) $value = [];
$_model = $model::find()->in($primaryId, $value);
} else {
$_model = $model::find()->where(['t1.' . $primaryId => $value]);
}
$this->_relation = $relation->bindIdentification($model, $_model);
$this->model = $model;
$this->primaryId = $primaryId;
$this->value = $value;
}
abstract public function get();
/**
* @param $name
* @return mixed
*/
public function __get($name)
{
if (empty($this->value)) {
return null;
}
return $this->get();
}
}