Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #2705 : countAllResults after find #2706

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,12 @@ public function __construct(ConnectionInterface &$db = null, ValidationInterface
* Fetches the row of database from $this->table with a primary key
* matching $id.
*
* @param mixed|array|null $id One primary key or an array of primary keys
* @param mixed|array|null $id One primary key or an array of primary keys
* @param boolean $reset Are we want to clear query builder values after find?
*
* @return array|object|null The resulting row of data, or null.
*/
public function find($id = null)
public function find($id = null, bool $reset = true)
{
$builder = $this->builder();

Expand All @@ -372,19 +373,19 @@ public function find($id = null)
if (is_array($id))
{
$row = $builder->whereIn($this->table . '.' . $this->primaryKey, $id)
->get();
->get(null, 0, $reset);
$row = $row->getResult($this->tempReturnType);
}
elseif (is_numeric($id) || is_string($id))
{
$row = $builder->where($this->table . '.' . $this->primaryKey, $id)
->get();
->get(null, 0, $reset);

$row = $row->getFirstRow($this->tempReturnType);
}
else
{
$row = $builder->get();
$row = $builder->get(null, 0, $reset);

$row = $row->getResult($this->tempReturnType);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1823,4 +1823,31 @@ public function testUndefinedMethodInBuilder()
->getBindings();
}

public function testFindEmptyCountAllResults()
{
$model = new UserModel($this->db);

$model->find();

$this->assertEquals(4, $model->countAllResults());
}

public function testFindIdCountAllResults()
{
$model = new UserModel($this->db);

$model->find(1, false);

$this->assertEquals(1, $model->countAllResults());
}

public function testFindArrayCountAllResults()
{
$model = new UserModel($this->db);

$model->find([1, 2], false);

$this->assertEquals(2, $model->countAllResults());
}

}
16 changes: 16 additions & 0 deletions user_guide_src/source/models/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ insert(), update(), delete() and more.

**find()**

The first parameter is key(s) value of the primary key.

The second parameter let you to reset or clear query builder values or not, by default it is set to `true` mean that query builder always being cleared.

Returns a single row where the primary key matches the value passed in as the first parameter::

$user = $userModel->find($user_id);
Expand All @@ -243,6 +247,18 @@ of just one::
If no parameters are passed in, will return all rows in that model's table, effectively acting
like findAll(), though less explicit.

**countAllResults()**

Return number of row(s) affected from find() method. In order to get proper result, you should pass second parameter as `false`.

$users = $userModel->find([1,2,3]);
$count = $userModel->countAllResults(); // produce all rows in UserModel

$users = $userModel->find([1,2,3], false);
$count = $userModel->countAllResults(); // produce 3 rows

.. note:: if you call `countAllResults` after using `find` with passing `$id`, always pass second parameter with `false` to avoid resetting query builder. See [related issue](https://github.com/codeigniter4/CodeIgniter4/issues/2705).

**findColumn()**

Returns null or an indexed array of column values::
Expand Down