catfan / Medoo

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

NULL to EMPTY_STRING #1082

Closed imrooster closed 1 year ago

imrooster commented 1 year ago

when i setup connection option as _PDO::ATTR_ORACLE_NULLS =>PDO::NULL_EMPTYSTRING

and run

$db->insert("table", [ "name" => "foo", "email" => "foo@bar.com", "phone" => "" ]);

the phone field still insert as empty string not null , WHY??

catfan commented 1 year ago

If you want to insert null data, try this:

$db->insert("table", [
    "name" => "foo",
    "email" => "foo@bar.com",
    "phone" => $phone === "" ? null : $phone
]); 
imrooster commented 1 year ago

so the _PDO::NULL_EMPTYSTRING doesn't work??

catfan commented 1 year ago

Because the empty string "" is still mapped as PDO::PARAM_STR inside https://github.com/catfan/Medoo/blob/master/src/Medoo.php#L744

Of course, you can just create your insert() method to handle those values and then call Medoo's insert().

function myInsert($data) {

    $result = [];

    foreach ($data as $key => $value) {
        $result[$key] = $value === "" ? null : $value
    }

    $database->insert("table", $result);
}