| <?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); |
| } |
| } |
| } |
|
|