123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace think\model;
- use think\db\Query;
- use think\Exception;
- use think\Model;
- abstract class Relation
- {
-
- protected $parent;
-
- protected $model;
-
- protected $query;
-
- protected $foreignKey;
-
- protected $localKey;
-
- protected $baseQuery;
-
- protected $selfRelation;
-
- public function getParent()
- {
- return $this->parent;
- }
-
- public function getModel()
- {
- return $this->query->getModel();
- }
-
- public function getQuery()
- {
- return $this->query;
- }
-
- public function selfRelation($self = true)
- {
- $this->selfRelation = $self;
- return $this;
- }
-
- public function isSelfRelation()
- {
- return $this->selfRelation;
- }
-
- protected function resultSetBuild($resultSet)
- {
- return (new $this->model)->toCollection($resultSet);
- }
- protected function getQueryFields($model)
- {
- $fields = $this->query->getOptions('field');
- return $this->getRelationQueryFields($fields, $model);
- }
- protected function getRelationQueryFields($fields, $model)
- {
- if ($fields) {
- if (is_string($fields)) {
- $fields = explode(',', $fields);
- }
- foreach ($fields as &$field) {
- if (false === strpos($field, '.')) {
- $field = $model . '.' . $field;
- }
- }
- } else {
- $fields = $model . '.*';
- }
- return $fields;
- }
-
- protected function baseQuery()
- {}
- public function __call($method, $args)
- {
- if ($this->query) {
-
- $this->baseQuery();
- $result = call_user_func_array([$this->query, $method], $args);
- if ($result instanceof Query) {
- return $this;
- } else {
- $this->baseQuery = false;
- return $result;
- }
- } else {
- throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
- }
- }
- }
|