top-think / top-think/think-orm

在php7.3及以上版本中,会出错

Open Beginner friendly
#325 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
437
Forks
188
PR merge metrics
No merged PRs in 30d

Description

代码位置 :\topthink\think-orm\src\db\BaseQuery.php
原代码:

public function field($field)
    {
        if (empty($field)) {
            return $this;
        } elseif ($field instanceof Raw) {
            $this->options['field'][] = $field;
            return $this;
        }

        if (is_string($field)) {
            if (preg_match('/[\<\'\"\(]/', $field)) {
                return $this->fieldRaw($field);
            }

            $field = array_map('trim', explode(',', $field));
        }

        if (true === $field) {
            // 获取全部字段
            $fields = $this->getTableFields();
            $field  = $fields ?: ['*'];
        }

        if (isset($this->options['field'])) {
            $field = array_merge((array) $this->options['field'], $field);
        }
        $this->options['field'] = array_unique($field);

        return $this;
    }

修改为

public function field($field)
  {
      if (empty($field)) {
          return $this;
      } elseif ($field instanceof Raw) {
          $this->options['field'][] = $field;
          return $this;
      }

      if (is_string($field)) {
          if (preg_match('/[\<\'\"\(]/', $field)) {
              return $this->fieldRaw($field);
          }

          $field = array_map('trim', explode(',', $field));
      }

      if (true === $field) {
          // 获取全部字段
          $fields = $this->getTableFields();
          $field  = $fields ?: ['*'];
      }

      if (isset($this->options['field'])) {
          $field = array_merge((array) $this->options['field'], $field);
      }
      $this->options['field'] = @array_unique($field);

      return $this;
  }

$this->options['field'] = array_unique($field); 这一行增加一个@,忽略报错,否则有时联表分组查询后,先查count,再查select会报错。如下图:
image

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open topthink/think-orm/src/db/BaseQuery.php and inspect BaseQuery::field(), especially the array_unique call after a count query followed by a select in a grouped join. Reproduce the reported PHP 7.3+ warning using the linked screenshot scenario, then verify that the follow-up select completes without the reported error and that existing ORM behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.