OneToOne.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\model\relation;
  12. use think\db\Query;
  13. use think\Exception;
  14. use think\Loader;
  15. use think\Model;
  16. use think\model\Relation;
  17. /**
  18. * Class OneToOne
  19. * @package think\model\relation
  20. *
  21. */
  22. abstract class OneToOne extends Relation
  23. {
  24. // 预载入方式 0 -JOIN 1 -IN
  25. protected $eagerlyType = 1;
  26. // 当前关联的JOIN类型
  27. protected $joinType;
  28. // 要绑定的属性
  29. protected $bindAttr = [];
  30. // 关联方法名
  31. protected $relation;
  32. /**
  33. * 设置join类型
  34. * @access public
  35. * @param string $type JOIN类型
  36. * @return $this
  37. */
  38. public function joinType($type)
  39. {
  40. $this->joinType = $type;
  41. return $this;
  42. }
  43. /**
  44. * 预载入关联查询(JOIN方式)
  45. * @access public
  46. * @param Query $query 查询对象
  47. * @param string $relation 关联名
  48. * @param string $subRelation 子关联
  49. * @param \Closure $closure 闭包条件
  50. * @param bool $first
  51. * @return void
  52. */
  53. public function eagerly(Query $query, $relation, $subRelation, $closure, $first)
  54. {
  55. $name = Loader::parseName(basename(str_replace('\\', '/', get_class($query->getModel()))));
  56. if ($first) {
  57. $table = $query->getTable();
  58. $query->table([$table => $name]);
  59. if ($query->getOptions('field')) {
  60. $field = $query->getOptions('field');
  61. $query->removeOption('field');
  62. } else {
  63. $field = true;
  64. }
  65. $query->field($field, false, $table, $name);
  66. $field = null;
  67. }
  68. // 预载入封装
  69. $joinTable = $this->query->getTable();
  70. $joinAlias = $relation;
  71. $query->via($joinAlias);
  72. if ($this instanceof BelongsTo) {
  73. $query->join([$joinTable => $joinAlias], $name . '.' . $this->foreignKey . '=' . $joinAlias . '.' . $this->localKey, $this->joinType);
  74. } else {
  75. $query->join([$joinTable => $joinAlias], $name . '.' . $this->localKey . '=' . $joinAlias . '.' . $this->foreignKey, $this->joinType);
  76. }
  77. if ($closure) {
  78. // 执行闭包查询
  79. call_user_func_array($closure, [ & $query]);
  80. // 使用withField指定获取关联的字段,如
  81. // $query->where(['id'=>1])->withField('id,name');
  82. if ($query->getOptions('with_field')) {
  83. $field = $query->getOptions('with_field');
  84. $query->removeOption('with_field');
  85. }
  86. } elseif (isset($this->option['field'])) {
  87. $field = $this->option['field'];
  88. }
  89. $query->field(isset($field) ? $field : true, false, $joinTable, $joinAlias, $relation . '__');
  90. }
  91. /**
  92. * 预载入关联查询(数据集)
  93. * @param array $resultSet
  94. * @param string $relation
  95. * @param string $subRelation
  96. * @param \Closure $closure
  97. * @return mixed
  98. */
  99. abstract protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure);
  100. /**
  101. * 预载入关联查询(数据)
  102. * @param Model $result
  103. * @param string $relation
  104. * @param string $subRelation
  105. * @param \Closure $closure
  106. * @return mixed
  107. */
  108. abstract protected function eagerlyOne(&$result, $relation, $subRelation, $closure);
  109. /**
  110. * 预载入关联查询(数据集)
  111. * @access public
  112. * @param array $resultSet 数据集
  113. * @param string $relation 当前关联名
  114. * @param string $subRelation 子关联名
  115. * @param \Closure $closure 闭包
  116. * @return void
  117. */
  118. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
  119. {
  120. if (1 == $this->eagerlyType) {
  121. // IN查询
  122. $this->eagerlySet($resultSet, $relation, $subRelation, $closure);
  123. } else {
  124. // 模型关联组装
  125. foreach ($resultSet as $result) {
  126. $this->match($this->model, $relation, $result);
  127. }
  128. }
  129. }
  130. /**
  131. * 预载入关联查询(数据)
  132. * @access public
  133. * @param Model $result 数据对象
  134. * @param string $relation 当前关联名
  135. * @param string $subRelation 子关联名
  136. * @param \Closure $closure 闭包
  137. * @return void
  138. */
  139. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  140. {
  141. if (1 == $this->eagerlyType) {
  142. // IN查询
  143. $this->eagerlyOne($result, $relation, $subRelation, $closure);
  144. } else {
  145. // 模型关联组装
  146. $this->match($this->model, $relation, $result);
  147. }
  148. }
  149. /**
  150. * 保存(新增)当前关联数据对象
  151. * @access public
  152. * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
  153. * @return Model|false
  154. */
  155. public function save($data)
  156. {
  157. if ($data instanceof Model) {
  158. $data = $data->getData();
  159. }
  160. $model = new $this->model;
  161. // 保存关联表数据
  162. $data[$this->foreignKey] = $this->parent->{$this->localKey};
  163. return $model->save($data) ? $model : false;
  164. }
  165. /**
  166. * 设置预载入方式
  167. * @access public
  168. * @param integer $type 预载入方式 0 JOIN查询 1 IN查询
  169. * @return $this
  170. */
  171. public function setEagerlyType($type)
  172. {
  173. $this->eagerlyType = $type;
  174. return $this;
  175. }
  176. /**
  177. * 获取预载入方式
  178. * @access public
  179. * @return integer
  180. */
  181. public function getEagerlyType()
  182. {
  183. return $this->eagerlyType;
  184. }
  185. /**
  186. * 绑定关联表的属性到父模型属性
  187. * @access public
  188. * @param mixed $attr 要绑定的属性列表
  189. * @return $this
  190. */
  191. public function bind($attr)
  192. {
  193. if (is_string($attr)) {
  194. $attr = explode(',', $attr);
  195. }
  196. $this->bindAttr = $attr;
  197. return $this;
  198. }
  199. /**
  200. * 获取绑定属性
  201. * @access public
  202. * @return array
  203. */
  204. public function getBindAttr()
  205. {
  206. return $this->bindAttr;
  207. }
  208. /**
  209. * 关联统计
  210. * @access public
  211. * @param Model $result 数据对象
  212. * @param \Closure $closure 闭包
  213. * @return integer
  214. */
  215. public function relationCount($result, $closure)
  216. {
  217. }
  218. /**
  219. * 一对一 关联模型预查询拼装
  220. * @access public
  221. * @param string $model 模型名称
  222. * @param string $relation 关联名
  223. * @param Model $result 模型对象实例
  224. * @return void
  225. */
  226. protected function match($model, $relation, &$result)
  227. {
  228. // 重新组装模型数据
  229. foreach ($result->getData() as $key => $val) {
  230. if (strpos($key, '__')) {
  231. list($name, $attr) = explode('__', $key, 2);
  232. if ($name == $relation) {
  233. $list[$name][$attr] = $val;
  234. unset($result->$key);
  235. }
  236. }
  237. }
  238. if (isset($list[$relation])) {
  239. $relationModel = new $model($list[$relation]);
  240. $relationModel->setParent(clone $result);
  241. $relationModel->isUpdate(true);
  242. if (!empty($this->bindAttr)) {
  243. $this->bindAttr($relationModel, $result, $this->bindAttr);
  244. }
  245. } else {
  246. $relationModel = null;
  247. }
  248. $result->setRelation(Loader::parseName($relation), $relationModel);
  249. }
  250. /**
  251. * 绑定关联属性到父模型
  252. * @access protected
  253. * @param Model $model 关联模型对象
  254. * @param Model $result 父模型对象
  255. * @param array $bindAttr 绑定属性
  256. * @return void
  257. * @throws Exception
  258. */
  259. protected function bindAttr($model, &$result, $bindAttr)
  260. {
  261. foreach ($bindAttr as $key => $attr) {
  262. $key = is_numeric($key) ? $attr : $key;
  263. if (isset($result->$key)) {
  264. throw new Exception('bind attr has exists:' . $key);
  265. } else {
  266. $result->setAttr($key, $model ? $model->$attr : null);
  267. }
  268. }
  269. }
  270. /**
  271. * 一对一 关联模型预查询(IN方式)
  272. * @access public
  273. * @param object $model 关联模型对象
  274. * @param array $where 关联预查询条件
  275. * @param string $key 关联键名
  276. * @param string $relation 关联名
  277. * @param string $subRelation 子关联
  278. * @param bool|\Closure $closure
  279. * @return array
  280. */
  281. protected function eagerlyWhere($model, $where, $key, $relation, $subRelation = '', $closure = false)
  282. {
  283. $this->baseQuery = true;
  284. // 预载入关联查询 支持嵌套预载入
  285. if ($closure) {
  286. call_user_func_array($closure, [ & $model]);
  287. if ($field = $model->getOptions('with_field')) {
  288. $model->field($field)->removeOption('with_field');
  289. }
  290. }
  291. $list = $model->where($where)->with($subRelation)->select();
  292. // 组装模型数据
  293. $data = [];
  294. foreach ($list as $set) {
  295. $data[$set->$key] = $set;
  296. }
  297. return $data;
  298. }
  299. /**
  300. * 创建关联统计子查询
  301. * @access public
  302. * @param \Closure $closure 闭包
  303. * @param string $name 统计数据别名
  304. * @return string
  305. */
  306. public function getRelationCountQuery($closure, &$name = null)
  307. {
  308. throw new Exception('relation not support: withCount');
  309. }
  310. }