Skip to content

Commit d0d8d59

Browse files
committed
改进join查询方法多次调用
1 parent 96c5f52 commit d0d8d59

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/db/concern/JoinAndViewQuery.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@ trait JoinAndViewQuery
3333
*/
3434
public function join(array | string | Raw $join, ?string $condition = null, string $type = 'INNER', array $bind = [])
3535
{
36-
$table = $this->getJoinTable($join);
36+
$table = $this->getJoinTable($join, $alias);
3737

3838
if (!empty($bind) && $condition) {
3939
$this->bindParams($condition, $bind);
4040
}
4141

42-
$this->options['join'][] = [$table, strtoupper($type), $condition];
42+
if ($alias) {
43+
$this->options['join'][$alias] = [$table, strtoupper($type), $condition];
44+
} else {
45+
$this->options['join'][] = [$table, strtoupper($type), $condition];
46+
}
4347

4448
return $this;
4549
}

src/db/concern/WhereQuery.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,14 @@ protected function parseWhereExp(string $logic, $field, $op, $condition, array $
453453
{
454454
$logic = strtoupper($logic);
455455

456+
// 字段映射
457+
$map = $this->getOption('field_map', []);
458+
if (is_string($field) && isset($map[$field])) {
459+
$field = $map[$field];
460+
}
461+
456462
// 处理 via
457-
if (is_string($field) && !empty($this->options['via']) && !str_contains($field, '.')) {
463+
if (is_string($field) && !empty($this->options['via']) && !str_contains($field, '.') && !str_contains($field, '->')) {
458464
$field = $this->options['via'] . '.' . $field;
459465
}
460466

@@ -522,19 +528,15 @@ protected function parseWhereItem(string $logic, $field, $op, $condition, array
522528
array_unshift($param, $field);
523529
return $param;
524530
}
525-
// 获取字段映射
526-
$alias = $this->getFieldMap($field);
527-
if ($alias) {
528-
$field = $alias;
529-
if (strpos($alias, '->')) {
530-
[$relation, $field] = explode('->', $alias, 2);
531-
532-
$type = $this->getFieldType($relation);
533-
if (is_null($type)) {
534-
// 自动关联查询
535-
$this->hasWhere($relation, [[$field , is_null($condition) ? '=' : $op, $condition ?? $op]]);
536-
return [];
537-
}
531+
532+
if (is_string($field) && strpos($field, '->')) {
533+
[$relation, $attr] = explode('->', $field, 2);
534+
535+
$type = $this->getFieldType($relation);
536+
if (is_null($type)) {
537+
// 自动关联查询
538+
$this->hasWhere($relation, [[$attr , is_null($condition) ? '=' : $op, $condition ?? $op]]);
539+
return [];
538540
}
539541
}
540542

0 commit comments

Comments
 (0)