OneToOne.php 10 KB

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