1

我正在使用 Codeigniter 4 查询生成器。以下是从数据库中检索数据的代码

    public function get_data()
    {
        $where = [
            'username' => 'admin'
        ];
        $this->_builder = $this->_db->table('pf_user_master s');
        $this->_builder->join('pf_role_master r', 'r.role_id = s.role_id');
        $this->_builder->select('username, first_name, last_name, mobile, email, r.role_name, s.status');
        if (is_array($where) && !empty($where)) {
            $this->_builder->where($where);
        }
        $first= $this->_builder->get()->getResultArray();
        print_r($first);
        $second= $this->_builder->get()->getResultArray();
        print_r($second);
        exit;
    }

我得到以下输出:

在变量 $first 中,我得到了预期的输出

Array
(
    [0] => Array
        (
            [username] => admin
            [first_name] => Fisrt
            [last_name] => Last
            [mobile] => 
            [email] => first.last@gmail.com
            [role_name] => Admin
            [status] => 1
        )

)

但是在变量 $second 中,Query Builder 会丢失 SELECT、WHERE 条件以及 JOIN。并按如下方式打印输出:

Array
(
    [0] => Array
        (
            [user_id] => 1
            [username] => admin
            [password] => 21232f297a57a5a743894a0e4a801fc3
            [first_name] => First
            [last_name] => Last
            [mobile] => 
            [email] => first.last@gmail.com
            [role_id] => 1
            [status] => 1
            [created_by] => 
            [created_date] => 2020-09-08 19:30:52
            [updated_by] => 
            [updated_date] => 2020-09-08 19:32:42
        )

    [1] => Array
        (
            [user_id] => 2
            [username] => superadmin
            [password] => 21232f297a57a5a743894a0e4a801fc3
            [first_name] => NewFirst
            [last_name] => NewLast
            [mobile] => 
            [email] => new.new@gmail.com
            [role_id] => 1
            [status] => 1
            [created_by] => 
            [created_date] => 2020-09-08 21:51:42
            [updated_by] => 
            [updated_date] => 
        )

)

4

2 回答 2

0

我没有对此进行测试,它仅来自阅读文档。

在 CodeIgniter 文档中 get() 有以下参数

get([$limit = NULL[, $offset = NULL[, $reset = TRUE]]]])

参考:https ://codeigniter.com/user_guide/database/query_builder.html#get

如果你要使用

$first= $this->_builder->get(NULL,NULL,FALSE)->getResultArray();

然后您的代码将变为:

public function get_data()
{
    $where = [
        'username' => 'admin'
    ];
    $this->_builder = $this->_db->table('pf_user_master s');
    $this->_builder->join('pf_role_master r', 'r.role_id = s.role_id');
    $this->_builder->select('username, first_name, last_name, mobile, email, r.role_name, s.status');
    if (is_array($where) && !empty($where)) {
        $this->_builder->where($where);
    }
    $first= $this->_builder->get(NULL,NULL,FALSE)->getResultArray();
    print_r($first);
    $second= $this->_builder->get()->getResultArray();
    print_r($second);
    exit;
}

当然,如果您要第三次执行此操作,则第二次实例应该会导致重置。

代码就是文档(在大多数情况下),所以我们有

在 CodeIgniter 4.04 - /system/Database/BaseBuilder.php - Line 1824 代码是

/**
     * Get
     *
     * Compiles the select statement based on the other functions called
     * and runs the query
     *
     * @param integer $limit  The limit clause
     * @param integer $offset The offset clause
     * @param boolean $reset  Are we want to clear query builder values?
     *
     * @return ResultInterface
     */
    public function get(int $limit = null, int $offset = 0, bool $reset = true)
    {
        if (! is_null($limit))
        {
            $this->limit($limit, $offset);
        }

        $result = $this->testMode
            ? $this->getCompiledSelect($reset)
            : $this->db->query($this->compileSelect(), $this->binds, false);

        if ($reset === true)
        {
            $this->resetSelect();

            // Clear our binds so we don't eat up memory
            $this->binds = [];
        }

        return $result;
    }

因此,$reset 的默认设置将“清除”您所观察到的内容。

于 2020-09-10T09:47:54.783 回答
-1

正如TimBrownlaw提到的, 我更改 $first= $this->_builder->get()->getResultArray();$first= $this->_builder->get(NULL, 0 , false)->getResultArray(); 它,它对我有用,因为它没有重置查询,因为它的第三个参数get()是用于重置查询

于 2020-09-11T11:09:11.580 回答