<?php<liu21st@gmail.com>
namespace think\model\relation;
use Closure;
use think\db\BaseQuery as Query;
use think\db\exception\DbException as Exception;
use think\helper\Str;
use think\Model;
use think\model\Relation;
abstract class OneToOne extends Relation
{
protected $joinType = 'INNER';
protected $bindAttr = [];
protected $relation;
public function joinType(string $type)
{
$this->joinType = $type;
return $this;
}
public function eagerly(Query $query, string $relation, $field = true, string $joinType = '', Closure $closure = null, bool $first = false): void
{
$name = Str::snake(class_basename($this->parent));
if ($first) {
$table = $query->getTable();
$query->table([$table => $name]);
if ($query->getOptions('field')) {
$masterField = $query->getOptions('field');
$query->removeOption('field');
} else {
$masterField = true;
}
$query->tableField($masterField, $table, $name);
}
$joinTable = $this->query->getTable();
$joinAlias = $relation;
$joinType = $joinType ?: $this->joinType;
$query->via($joinAlias);
if ($this instanceof BelongsTo) {
$joinOn = $name . '.' . $this->foreignKey . '=' . $joinAlias . '.' . $this->localKey;
} else {
$joinOn = $name . '.' . $this->localKey . '=' . $joinAlias . '.' . $this->foreignKey;
}
if ($closure) {
$closure($this->getClosureType($closure));
if ($this->withField) {
$field = $this->withField;
}
}
$query->join([$joinTable => $joinAlias], $joinOn, $joinType)
->tableField($field, $joinTable, $joinAlias, $relation . '__');
}
abstract protected function eagerlySet(array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null);
abstract protected function eagerlyOne(Model $result, string $relation, array $subRelation = [], Closure $closure = null);
public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = [], bool $join = false): void
{
if ($join) {
foreach ($resultSet as $result) {
$this->match($this->model, $relation, $result);
}
} else {
$this->eagerlySet($resultSet, $relation, $subRelation, $closure, $cache);
}
}
public function eagerlyResult(Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = [], bool $join = false): void
{
if ($join) {
$this->match($this->model, $relation, $result);
} else {
$this->eagerlyOne($result, $relation, $subRelation, $closure, $cache);
}
}
public function save($data, bool $replace = true)
{
if ($data instanceof Model) {
$data = $data->getData();
}
$model = new $this->model;
$data[$this->foreignKey] = $this->parent->{$this->localKey};
return $model->replace($replace)->save($data) ? $model : false;
}
public function bind(array $attr)
{
$this->bindAttr = $attr;
return $this;
}
public function getBindAttr(): array
{
return $this->bindAttr;
}
protected function match(string $model, string $relation, Model $result): void
{
foreach ($result->getData() as $key => $val) {
if (strpos($key, '__')) {
[$name, $attr] = explode('__', $key, 2);
if ($name == $relation) {
$list[$name][$attr] = $val;
unset($result->$key);
}
}
}
if (isset($list[$relation])) {
$array = array_unique($list[$relation]);
if (count($array) == 1 && null === current($array)) {
$relationModel = null;
} else {
$relationModel = new $model($list[$relation]);
$relationModel->setParent(clone $result);
$relationModel->exists(true);
}
if (!empty($this->bindAttr)) {
$this->bindAttr($result, $relationModel);
}
} else {
$relationModel = null;
}
$result->setRelation($relation, $relationModel);
}
protected function bindAttr(Model $result, Model $model = null): void
{
foreach ($this->bindAttr as $key => $attr) {
$key = is_numeric($key) ? $attr : $key;
$value = $result->getOrigin($key);
if (!is_null($value)) {
throw new Exception('bind attr has exists:' . $key);
}
$result->setAttr($key, $model ? $model->$attr : null);
}
}
protected function eagerlyWhere(array $where, string $key, array $subRelation = [], Closure $closure = null, array $cache = [])
{
if ($closure) {
$this->baseQuery = true;
$closure($this->getClosureType($closure));
}
if ($this->withField) {
$this->query->field($this->withField);
}
if ($this->query->getOptions('order')) {
$this->query->group($key);
}
$list = $this->query
->where($where)
->with($subRelation)
->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null)
->select();
$data = [];
foreach ($list as $set) {
if (!isset($data[$set->$key])) {
$data[$set->$key] = $set;
}
}
return $data;
}
}