赛亿官网

OneToOne.php 9.7KB

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