Comments (20)
whether there is an abstraction layer between it or not, one expects a count function to return a number, not a string. This is the most intuitive result.
from db.
I would say that we should do it a long time ago. W can safely convert count()
result to int, so there is no technical problem here.
from db.
The only con is that we'll have int|string as a type hint :(
Numerical string will be returned only in edge cases (when you need integer bigger than your environment can handle). We could:
- Laravel style - f*ck edge cases and always cast to int.
- Remove
string
from typehint - in 99.9999% cases you will get int anyway, sostring
in typehint may give more harm than good.
@ptz-nerf This is not a code sample. How do you want to use ===
on comparing result with integer that cannot be represented as integer?
from db.
I'd also expect an integer here.
From https://stackoverflow.com/questions/670662/whats-the-maximum-size-for-an-int-in-php even on 32bit systems this would be limited to ~2 billion, which is quite large and on 64bit to ~9 quintillion.
from db.
This is normal because PDO does always return strings regardless what value it was on DB side. Because PHP is not stricly typed this should not be a problem.
from db.
If you are doing strict comparisons of result and some integer value it is. But of course, it is a question do you really need to do strict comparison at all... I guess it is not a "real" problem. Thanks for info.
from db.
You should only do strict comparison if you are really sure or really need to. This is not such a case.
from db.
if you are really sure
When can count() return not integer number? string|int return type is missleading: people expect to see the number, suspecting that string will be return in a rare strange situation. Everything is quite the opposite, int (0) will be return only if emulateExecution is true...
from db.
#14704
from db.
Hmm... yes. We can't do it :(
from db.
32bit ints?
from db.
With approach from #14704 it will be represented as numerical string.
from db.
But that's quite inconsistent. It's better to cast to string than to have two types. Developers will use ===
and will be quite surprised when the project DB would exceed 32bit int.
from db.
Developers will use === and will be quite surprised when the project DB would exceed 32bit int.
Can you give a code sample, when this will happen? You will get string only if result cannot be converted to integer, so how do you want to use ===
in this case?
BTW: Developers are already surprised that count()
returns string. And such inconsistency already exists in AR.
from db.
#14704
Indeed, string is better.
from db.
Actually what @rob006 proposes makes sense in practice. While under a 100 it may make sense to strictly compare counts, more than several thousands should always be done with >
or <
and for these operators it doesn't really make any difference if operands are strings or ints.
The only con is that we'll have int|string
as a type hint :(
from db.
Let's cast to int
as @rob006 originally proposed. @yiisoft/core-developers objections?
from db.
Given that the result can be of different types on any numbers (yiisoft/yii2#14704 (comment)), it is better to have int
(with @rob006 check) in most situations. On x64 it is insignificant. string
really surprised me at the first moment.
from db.
No objections
from db.
done.
from db.
Related Issues (20)
- `Query::count()` can return `int|string` but always returns `int` HOT 1
- `Query::leftJoin` - a simpler way to quote join column? HOT 3
- Remove `$withColumn` parameter from `getTableNameParts()` HOT 3
- Remove method `TableSchema::compositeForeignKey()` HOT 1
- `CommandInterface::getRawSQL()` - using in production / alternative HOT 12
- `Schema::hasTableName()` method is missing HOT 1
- Allow scalars `int|float|bool` in `Query::select()` HOT 1
- Changes before release 2.0.0
- Allow use `DMLQueryBuilderInterface::batchInsert()` without `$columns` HOT 4
- Add posibility to drop table `IF EXISTS` and `CASCADE`
- Allow pass `ExpressionInterface` as alias to `QueryPartsInterface::withQuery()` HOT 1
- No usage of `AbstractCommand::requireTransaction()` HOT 3
- Needs to refactor `getRawSql()`
- Bug in `Command::insertWithReturningPks()` with empty values
- `Command::upsertWithReturningPks()` required for `ActiveRecord::upsert()`
- update irc link
- Bug when try to `Command::update()` using `Expression`s in values or in where condition
- Add ability to create like condition as `%value`, `value%` and `value` without `%`
- Add new method `SchemaInterface::getUserDefinedTypes()`
- Add ability to create `SELECT` with `FOR UPDATE`
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 db.