catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

Casting Null to Int gives 0. #721

Closed utkant closed 6 years ago

utkant commented 6 years ago

Hello!

I have a select where i cast the ids to Int in the select, but it casts the null value to 0 which is not what I want. Is this a bug or is this supposed to be like this?

$fields = ["id [Int]"]; $this->database->get("table", $fields)

If id is NULL then this gives it the value 0 and not null.

mozart77 commented 6 years ago

If you cast NULL to Int what do you expect?

utkant commented 6 years ago

If I have a table where id can be NULL or an integer, and I want the result NULL when NULL and not 0 as in integer 0, since 0 can be a valid id, how can I achieve? I need to add code for that. I believe that this is a bug.

NULL (id) !== 0(id) ...

mozart77 commented 6 years ago

Don't cast then

utkant commented 6 years ago

This means that all fields that have id columns containing NULL values cannot use casting and needs extra processing. I would love it if medoo added a [Id] casting option where this was fixed. Possibility?

utkant commented 6 years ago

Something like this in the mapping switch case 'Id': $stack[ $column_key ] = is_null($data[ $column_key ]) ? null : (int) $data[ $column_key ]; break;

catfan commented 6 years ago

Yes, this is a bug. The null value should return as Null not 0. I made a bug fix for this. d28ae10