Skip to content

Commit 005f1c1

Browse files
committed
改进视图模型无需指定together自动关联写入
改进关联模型使用外键去重后查询
1 parent 3bb5e62 commit 005f1c1

File tree

7 files changed

+22
-9
lines changed

7 files changed

+22
-9
lines changed

src/model/View.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,22 +331,31 @@ protected function convertData(): array
331331
$mapping = $this->getOption('autoMappingAlias', []);
332332
$data = $this->getData();
333333
$item = [];
334+
$together = [];
334335
foreach ($properties as $key => $field) {
335336
if (strpos($field, '->')) {
336-
$fields = explode('->', $field);
337-
$last = array_pop($fields);
338-
$target = $this->model();
337+
$fields = explode('->', $field);
338+
$together[] = current($fields);
339+
$last = array_pop($fields);
340+
$target = $this->model();
339341
foreach ($fields as $attr) {
340342
$target = $target->$attr;
341343
}
342344
$target->$last = $data[$key];
343345
} elseif (is_int($key) && isset($mapping[$field])) {
344346
[$relation] = explode('->', $mapping[$field]);
347+
$together[] = $relation;
345348
$this->model()->$relation->$field = $data[$field];
346349
} else {
347350
$item[$field] = $data[is_int($key) ? $field : $key];
348351
}
349352
}
353+
354+
if (!empty($together)) {
355+
// 自动关联写入
356+
$together = array_unique($together);
357+
$this->model()->together($together);
358+
}
350359
return $item;
351360
}
352361

@@ -378,6 +387,10 @@ protected function validate(array $data = [], array $allow = []): array
378387
*/
379388
public function save(): bool
380389
{
390+
if (!$this->getOption('allowWrite', false)) {
391+
return false;
392+
}
393+
381394
// 根据映射关系转换为实际模型数据
382395
$data = $this->convertData();
383396
// 处理自动时间字段数据

src/model/relation/BelongsTo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ protected function eagerlySet(array &$resultSet, string $relation, array $subRel
228228
$defaultModel = $this->getDefaultModel($default);
229229

230230
$data = $this->eagerlyWhere([
231-
[$localKey, 'in', $range],
231+
[$localKey, 'in', array_unique($range)],
232232
], $localKey, $subRelation, $closure, $cache);
233233

234234
// 动态绑定参数

src/model/relation/BelongsToMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub
314314
if (!empty($range)) {
315315
// 查询关联数据
316316
$data = $this->eagerlyManyToMany([
317-
['pivot.' . $localKey, 'in', $range],
317+
['pivot.' . $localKey, 'in', array_unique($range)],
318318
], $subRelation, $closure, $cache);
319319

320320
// 关联数据封装

src/model/relation/HasMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub
9191

9292
if (!empty($range)) {
9393
$data = $this->eagerlyOneToMany([
94-
[$this->foreignKey, 'in', $range],
94+
[$this->foreignKey, 'in', array_unique($range)],
9595
], $subRelation, $closure, $cache);
9696

9797
// 关联数据封装

src/model/relation/HasManyThrough.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub
177177
$this->query->removeWhereField($foreignKey);
178178

179179
$data = $this->eagerlyWhere([
180-
[$this->foreignKey, 'in', $range],
180+
[$this->foreignKey, 'in', array_unique($range)],
181181
], $foreignKey, $subRelation, $closure, $cache);
182182

183183
// 关联数据封装

src/model/relation/MorphMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub
170170

171171
if (!empty($range)) {
172172
$where = [
173-
[$morphKey, 'in', $range],
173+
[$morphKey, 'in', array_unique($range)],
174174
[$morphType, '=', $type],
175175
];
176176
$data = $this->eagerlyMorphToMany($where, $subRelation, $closure, $cache);

src/model/relation/MorphToMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub
106106
if (!empty($range)) {
107107
// 查询关联数据
108108
$data = $this->eagerlyManyToMany([
109-
['pivot.' . $this->localKey, 'in', $range],
109+
['pivot.' . $this->localKey, 'in', array_unique($range)],
110110
['pivot.' . $this->morphType, '=', $this->morphClass],
111111
], $subRelation, $closure, $cache);
112112

0 commit comments

Comments
 (0)