We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to set empty vars to NULL?

Hi,

I searched the web hours for this but now I have to ask.

How can I set values to NULL if there are empty? When I try to execute

$form->create();

I get the error "Exception: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'heart_rate' at row 1" because heart_rate is an empty string. How can I set it automatically to NULL?



8.1k

Just, for example,

$str = '';

echo (int)$str;

String conversion



14.4k

Just, for example,

$str = '';

echo (int)$str;

String conversion

This will lead to zeros in my database. I need NULLs.



79.0k
Accepted
answer
edited Jun '16

This is what I use for my custom models.

  //removes all NULL, FALSE and empty strings but leaves 0 (zero) values
            $buffer = array_filter($modelData, 'strlen');   //$modelData is an array


14.4k

This is what I use for my custom models.

 //removes all NULL, FALSE and empty strings but leaves 0 (zero) values
           $buffer = array_filter($modelData, 'strlen');   //$modelData is an array

That looks promising. Where do you get $modelData from?

It's out of scope of this topic (like I said, custom Models/ORM implementation).

You should focus on array_filter like I provided in example.