Codeigniter 4 Query Builder 在再次执行时丢失 SELECT 和 WHERE 条件
Codeigniter 4 Query Builder loses SELECT and WHERE condition while executing once more
我正在使用 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] =>
)
)
我没有对此进行测试,它仅来自于阅读文档。
在 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;
}
当然,如果您要第 3 次执行此操作,则第 2 次实例应该会导致重置。
代码就是文档(在大多数情况下)所以我们有
在 CodeIgniter 4.04 中 - /system/Database/BaseBuilder.php - 第 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 的默认值将“清除”您观察到的内容。
如TimBrownlaw所述
我变了
$first= $this->_builder->get()->getResultArray();
到
$first= $this->_builder->get(NULL, 0 , false)->getResultArray();
它对我有用,因为它没有重置查询,因为 get()
的第三个参数用于重置查询
我正在使用 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] =>
)
)
我没有对此进行测试,它仅来自于阅读文档。
在 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;
}
当然,如果您要第 3 次执行此操作,则第 2 次实例应该会导致重置。
代码就是文档(在大多数情况下)所以我们有
在 CodeIgniter 4.04 中 - /system/Database/BaseBuilder.php - 第 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 的默认值将“清除”您观察到的内容。
如TimBrownlaw所述
我变了
$first= $this->_builder->get()->getResultArray();
到
$first= $this->_builder->get(NULL, 0 , false)->getResultArray();
它对我有用,因为它没有重置查询,因为 get()
的第三个参数用于重置查询