123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace think\model\relation;
- use think\db\Query;
- use think\Exception;
- use think\Loader;
- use think\Model;
- use think\model\Relation;
- class MorphOne extends Relation
- {
-
- protected $morphKey;
- protected $morphType;
-
- protected $type;
-
- public function __construct(Model $parent, $model, $morphKey, $morphType, $type)
- {
- $this->parent = $parent;
- $this->model = $model;
- $this->type = $type;
- $this->morphKey = $morphKey;
- $this->morphType = $morphType;
- $this->query = (new $model)->db();
- }
-
- public function getRelation($subRelation = '', $closure = null)
- {
- if ($closure) {
- call_user_func_array($closure, [ & $this->query]);
- }
- $relationModel = $this->relation($subRelation)->find();
- if ($relationModel) {
- $relationModel->setParent(clone $this->parent);
- }
- return $relationModel;
- }
-
- public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER')
- {
- return $this->parent;
- }
-
- public function hasWhere($where = [], $fields = null)
- {
- throw new Exception('relation not support: hasWhere');
- }
-
- public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
- {
- $morphType = $this->morphType;
- $morphKey = $this->morphKey;
- $type = $this->type;
- $range = [];
- foreach ($resultSet as $result) {
- $pk = $result->getPk();
-
- if (isset($result->$pk)) {
- $range[] = $result->$pk;
- }
- }
- if (!empty($range)) {
- $data = $this->eagerlyMorphToOne([
- $morphKey => ['in', $range],
- $morphType => $type,
- ], $relation, $subRelation, $closure);
-
- $attr = Loader::parseName($relation);
-
- foreach ($resultSet as $result) {
- if (!isset($data[$result->$pk])) {
- $relationModel = null;
- } else {
- $relationModel = $data[$result->$pk];
- $relationModel->setParent(clone $result);
- $relationModel->isUpdate(true);
- }
- $result->setRelation($attr, $relationModel);
- }
- }
- }
-
- public function eagerlyResult(&$result, $relation, $subRelation, $closure)
- {
- $pk = $result->getPk();
- if (isset($result->$pk)) {
- $pk = $result->$pk;
- $data = $this->eagerlyMorphToOne([
- $this->morphKey => $pk,
- $this->morphType => $this->type,
- ], $relation, $subRelation, $closure);
- if (isset($data[$pk])) {
- $relationModel = $data[$pk];
- $relationModel->setParent(clone $result);
- $relationModel->isUpdate(true);
- } else {
- $relationModel = null;
- }
- $result->setRelation(Loader::parseName($relation), $relationModel);
- }
- }
-
- protected function eagerlyMorphToOne($where, $relation, $subRelation = '', $closure = false)
- {
-
- if ($closure) {
- call_user_func_array($closure, [ & $this]);
- }
- $list = $this->query->where($where)->with($subRelation)->find();
- $morphKey = $this->morphKey;
-
- $data = [];
- foreach ($list as $set) {
- $data[$set->$morphKey][] = $set;
- }
- return $data;
- }
-
- public function save($data)
- {
- if ($data instanceof Model) {
- $data = $data->getData();
- }
-
- $pk = $this->parent->getPk();
- $data[$this->morphKey] = $this->parent->$pk;
- $data[$this->morphType] = $this->type;
- $model = new $this->model();
- return $model->save() ? $model : false;
- }
-
- public function make($data = [])
- {
- if ($data instanceof Model) {
- $data = $data->getData();
- }
-
- $pk = $this->parent->getPk();
- $data[$this->morphKey] = $this->parent->$pk;
- $data[$this->morphType] = $this->type;
- return new $this->model($data);
- }
-
- protected function baseQuery()
- {
- if (empty($this->baseQuery) && $this->parent->getData()) {
- $pk = $this->parent->getPk();
- $map[$this->morphKey] = $this->parent->$pk;
- $map[$this->morphType] = $this->type;
- $this->query->where($map);
- $this->baseQuery = true;
- }
- }
-
- public function getRelationCountQuery($closure, &$name = null)
- {
- throw new Exception('relation not support: withCount');
- }
- }
|