Use Eloquent orm about the inconsistency between the PHP parameter type and the mysql field type, resulting in not going to the index, some thinking? I hope someone will solve the problem.

problem description

typical mobile phone number problem. The database varchar, mobile phone number is indexed, but php passes the parameter int
public function getByPhone ($phone) {

return $this->userFollow
    ->where("phone", $phone)
    ->get();

}
$this- > getByPhone (13845678889); / / the native sql printed by the index
is:
string (48) "select * from zp_user_follow where phone =?" Array (1) {[0] = > int (15000475201)}

$this- > getByPhone ("13845678889"); / / Walk index
print out the native sql as follows:
string (48) "select * from zp_user_follow where phone =?" Array (1) {[0] = > string (11) "15000475201"}

the environmental background of the problems and what methods you have tried

add the type
public function getByPhone ( string $phone before the variable) {

return $this->userFollow
    ->where("phone", $phone)
    ->get();

}
this is a mandatory constraint, but what if the programmer doesn"t write it? If you pass an array, it is ["phone" = > 13845678889] in the array; how to solve it?

related codes

laravel is the underlying code of the pdo,pdo binding parameter, which determines the type used by the third parameter
public function bindValues ($statement, $bindings)
{

foreach ($bindings as $key => $value) {
    $statement->bindValue(
        is_string($key) ? $key : $key + 1, $value,
        is_int($value) || is_float($value) ? PDO::PARAM_INT : PDO::PARAM_STR
    );
}

}

what result do you expect? What is the error message actually seen?

is there a way to automatically parse the query parameter type and match the database field type when querying without changing the underlying laravel, even if the programmer has made a mistake? if there is no way, we can only throw the problem to the programmer? Ask them to note that when writing code, declare that the type of query parameters should be the same as that of the database? What if they forget? The problem is enlarged a little bit, if the generation environment has a large amount of data, do not take the index, full table scan, if because of this problem, lead to slow sql, generation environment out of bug, deduction performance? Criticism?

is to throw the problem to people themselves. If we leave it to people, then we are used to finding a reason why we do not want to solve the problem by technical means because we are accustomed to stipulating that the agreement is greater than the configuration. It can still be solved by technical means, so we have to change the bottom layer, but why does the bottom layer of laravel achieve this? It itself throws the problem to us developers and requires us to implement the agreement. , I hope someone will solve the problem.

Oct.26,2021

Service layer handles type conversion of parameters


this has nothing to do with laravel.

The phone field in the

database is varchar, but the data you are querying is int, which leads to mysql implicit type conversion, so your index is invalid.

you need to consider these problems when designing the table, and make type constraints in the process of implementation


Yes! This is what programmers have to deal with. The usage of
is the way of PDO and has nothing to do with laravel. The type of
mysql is defined by the developer, so it is up to the developer to decide which type to use.
this is something that the framework layer and the language layer have to solve. Even if you write sql, naked, someone will make mistakes.

from another point of view, this is actually the knowledge of the use of sql, but there are different rules for the use of sql, through the middle tier, which needs to be mastered by the user

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37ef2-2c0de.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37ef2-2c0de.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?