Comments (4)
A simple $size = $db->from('vw_escrituracao_fiscal')->count();
will fixit for you.
Anyway, this is not a bug, the behaviour is correct. You should not reuse the same object instance for doing different SQL operations. Please refer to the PHP documentation regarding objects and references.
from database.
I'm sorry but I disagree with creating another instance to count the records only.
This example I went through is simple, in a more complex situation, where I am using multiple where and inner joins this would make a code replication just for me to count the records to do a pagination.
<?php
$result = $db->from('vw_escrituracao_fiscal');
$result->where('date')->between('2019-11-10', '2019-11-17');
//various conditions where
$result->where ....
$result->where ...
$result->where ...
$result->where ...
$itens = $result->limit($params->limit)
->offset($params->offset)
->orderBy('id', $params->order_by ?? 'asc')
->select()
->fetchClass(ListNFSE::class)
->all();
// $size = $result->count();
you suggest i do that
<?php
$result = $db->from('vw_escrituracao_fiscal');
$result->where('date')->between('2019-11-10', '2019-11-17');
various conditions where
$result->where ....
$result->where ...
$result->where ...
$result->where ...
$size = $result->count();
from database.
You are not required to replicate your code, but you must use a different object instance for each operation. Something like bellow:
// Filter
$query = $db->from('my_database')
->where('column_a')->is($someValue)
->where('column_b')->is($otherValue);
// obtain new object instance
$counter = clone $query;
// Sort, paginate and fetch
$results = $query->limit($limit)
->offset($offset)
->orderBy('column_c')
->select()
->all();
// Count
$count = $counter->count();
Of course, it is not necessary to store the cloned instance into a variable. You could use it directly
// obtain new object instance & count
$count = (clone $query)->count();
from database.
thanks
from database.
Related Issues (20)
- Set charset for result to utf8? HOT 2
- Adding multiple data with a single query HOT 3
- How to get raw before sql limit function? HOT 1
- Query "find and replace" HOT 5
- Feature request: possibility to use some complicated expression in select statement HOT 2
- Documentation needs update (Transactions page) HOT 1
- Get full raw SQL in 4.* before execution HOT 1
- Multiple conditions if empty records come HOT 4
- Transactions with rollback HOT 1
- Problem with MID HOT 3
- enum type HOT 2
- undefined method all
- Package does not install with PHP 8
- Timestamp field with default value on creation HOT 3
- php 8.0 support HOT 1
- Insert records in batch
- Is possible to use Postgresql Jsonb data type with Opis?
- Deprecation warning - PHP 8.1 HOT 8
- aggregate function in select
- Package maintenance?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from database.