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