OneToOne.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. //lizhen
  73. if(strpos($this->foreignKey,'.')){
  74. $new_foreignKey = $this->foreignKey;
  75. }else{
  76. $new_foreignKey = $name . '.' . $this->foreignKey;
  77. }
  78. if ($this instanceof BelongsTo) {
  79. $query->join([$joinTable => $joinAlias], $new_foreignKey . '=' . $joinAlias . '.' . $this->localKey, $this->joinType);
  80. //$query->join([$joinTable => $joinAlias], $name . '.' . $this->foreignKey . '=' . $joinAlias . '.' . $this->localKey, $this->joinType);
  81. } else {
  82. $query->join([$joinTable => $joinAlias], $name . '.' . $this->localKey . '=' . $joinAlias . '.' . $this->foreignKey, $this->joinType);
  83. }
  84. //lizhen
  85. if ($closure) {
  86. // 执行闭包查询
  87. call_user_func_array($closure, [ & $query]);
  88. // 使用withField指定获取关联的字段,如
  89. // $query->where(['id'=>1])->withField('id,name');
  90. if ($query->getOptions('with_field')) {
  91. $field = $query->getOptions('with_field');
  92. $query->removeOption('with_field');
  93. }
  94. } elseif (isset($this->option['field'])) {
  95. $field = $this->option['field'];
  96. }
  97. $query->field(isset($field) ? $field : true, false, $joinTable, $joinAlias, $relation . '__');
  98. }
  99. /**
  100. * 预载入关联查询(数据集)
  101. * @param array $resultSet
  102. * @param string $relation
  103. * @param string $subRelation
  104. * @param \Closure $closure
  105. * @return mixed
  106. */
  107. abstract protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure);
  108. /**
  109. * 预载入关联查询(数据)
  110. * @param Model $result
  111. * @param string $relation
  112. * @param string $subRelation
  113. * @param \Closure $closure
  114. * @return mixed
  115. */
  116. abstract protected function eagerlyOne(&$result, $relation, $subRelation, $closure);
  117. /**
  118. * 预载入关联查询(数据集)
  119. * @access public
  120. * @param array $resultSet 数据集
  121. * @param string $relation 当前关联名
  122. * @param string $subRelation 子关联名
  123. * @param \Closure $closure 闭包
  124. * @return void
  125. */
  126. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
  127. {
  128. if (1 == $this->eagerlyType) {
  129. // IN查询
  130. $this->eagerlySet($resultSet, $relation, $subRelation, $closure);
  131. } else {
  132. // 模型关联组装
  133. foreach ($resultSet as $result) {
  134. $this->match($this->model, $relation, $result);
  135. }
  136. }
  137. }
  138. /**
  139. * 预载入关联查询(数据)
  140. * @access public
  141. * @param Model $result 数据对象
  142. * @param string $relation 当前关联名
  143. * @param string $subRelation 子关联名
  144. * @param \Closure $closure 闭包
  145. * @return void
  146. */
  147. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  148. {
  149. if (1 == $this->eagerlyType) {
  150. // IN查询
  151. $this->eagerlyOne($result, $relation, $subRelation, $closure);
  152. } else {
  153. // 模型关联组装
  154. $this->match($this->model, $relation, $result);
  155. }
  156. }
  157. /**
  158. * 保存(新增)当前关联数据对象
  159. * @access public
  160. * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
  161. * @return Model|false
  162. */
  163. public function save($data)
  164. {
  165. if ($data instanceof Model) {
  166. $data = $data->getData();
  167. }
  168. $model = new $this->model;
  169. // 保存关联表数据
  170. $data[$this->foreignKey] = $this->parent->{$this->localKey};
  171. return $model->save($data) ? $model : false;
  172. }
  173. /**
  174. * 设置预载入方式
  175. * @access public
  176. * @param integer $type 预载入方式 0 JOIN查询 1 IN查询
  177. * @return $this
  178. */
  179. public function setEagerlyType($type)
  180. {
  181. $this->eagerlyType = $type;
  182. return $this;
  183. }
  184. /**
  185. * 获取预载入方式
  186. * @access public
  187. * @return integer
  188. */
  189. public function getEagerlyType()
  190. {
  191. return $this->eagerlyType;
  192. }
  193. /**
  194. * 绑定关联表的属性到父模型属性
  195. * @access public
  196. * @param mixed $attr 要绑定的属性列表
  197. * @return $this
  198. */
  199. public function bind($attr)
  200. {
  201. if (is_string($attr)) {
  202. $attr = explode(',', $attr);
  203. }
  204. $this->bindAttr = $attr;
  205. return $this;
  206. }
  207. /**
  208. * 获取绑定属性
  209. * @access public
  210. * @return array
  211. */
  212. public function getBindAttr()
  213. {
  214. return $this->bindAttr;
  215. }
  216. /**
  217. * 关联统计
  218. * @access public
  219. * @param Model $result 数据对象
  220. * @param \Closure $closure 闭包
  221. * @return integer
  222. */
  223. public function relationCount($result, $closure)
  224. {
  225. }
  226. /**
  227. * 一对一 关联模型预查询拼装
  228. * @access public
  229. * @param string $model 模型名称
  230. * @param string $relation 关联名
  231. * @param Model $result 模型对象实例
  232. * @return void
  233. */
  234. protected function match($model, $relation, &$result)
  235. {
  236. // 重新组装模型数据
  237. foreach ($result->getData() as $key => $val) {
  238. if (strpos($key, '__')) {
  239. list($name, $attr) = explode('__', $key, 2);
  240. if ($name == $relation) {
  241. $list[$name][$attr] = $val;
  242. unset($result->$key);
  243. }
  244. }
  245. }
  246. if (isset($list[$relation])) {
  247. $relationModel = new $model($list[$relation]);
  248. $relationModel->setParent(clone $result);
  249. $relationModel->isUpdate(true);
  250. if (!empty($this->bindAttr)) {
  251. $this->bindAttr($relationModel, $result, $this->bindAttr);
  252. }
  253. } else {
  254. $relationModel = null;
  255. }
  256. $result->setRelation(Loader::parseName($relation), $relationModel);
  257. }
  258. /**
  259. * 绑定关联属性到父模型
  260. * @access protected
  261. * @param Model $model 关联模型对象
  262. * @param Model $result 父模型对象
  263. * @param array $bindAttr 绑定属性
  264. * @return void
  265. * @throws Exception
  266. */
  267. protected function bindAttr($model, &$result, $bindAttr)
  268. {
  269. foreach ($bindAttr as $key => $attr) {
  270. $key = is_numeric($key) ? $attr : $key;
  271. if (isset($result->$key)) {
  272. throw new Exception('bind attr has exists:' . $key);
  273. } else {
  274. $result->setAttr($key, $model ? $model->$attr : null);
  275. }
  276. }
  277. }
  278. /**
  279. * 一对一 关联模型预查询(IN方式)
  280. * @access public
  281. * @param object $model 关联模型对象
  282. * @param array $where 关联预查询条件
  283. * @param string $key 关联键名
  284. * @param string $relation 关联名
  285. * @param string $subRelation 子关联
  286. * @param bool|\Closure $closure
  287. * @return array
  288. */
  289. protected function eagerlyWhere($model, $where, $key, $relation, $subRelation = '', $closure = false)
  290. {
  291. $this->baseQuery = true;
  292. // 预载入关联查询 支持嵌套预载入
  293. if ($closure) {
  294. call_user_func_array($closure, [ & $model]);
  295. if ($field = $model->getOptions('with_field')) {
  296. $model->field($field)->removeOption('with_field');
  297. }
  298. }
  299. $list = $model->where($where)->with($subRelation)->select();
  300. // 组装模型数据
  301. $data = [];
  302. foreach ($list as $set) {
  303. $data[$set->$key] = $set;
  304. }
  305. return $data;
  306. }
  307. /**
  308. * 创建关联统计子查询
  309. * @access public
  310. * @param \Closure $closure 闭包
  311. * @param string $name 统计数据别名
  312. * @return string
  313. */
  314. public function getRelationCountQuery($closure, &$name = null)
  315. {
  316. throw new Exception('relation not support: withCount');
  317. }
  318. }