Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Setting a column back to NULL
  • Hello together

    Another question regarding the ORM :) :

    Sometimes i wanna be able to allow the user to set the value of a column to (db)NULL again or setting the value of a column initally to NULL.

    Using the query bulder it is possible to set someting to NULL

    (DB::update('table)->set(array('column'=>null))->where('Id','=',1)->execute())

    but if i try to assign NULL to a property of an ORM model, the generated query always contains an empty string ('') instead of NULL.

    Eg.

    $test = Test::find(1);
    $test->Column = null;
    $test->save();

    or

    $test = new Test();
    $test->Column = null;
    $test->save();

    results in "UPDATE test SET Column='' WHERE Id=1" instead of "UPDATE test SET Column = NULL WHERE Id=1".

    Is there a way to tell Fuel to use NULL instead of '' inside the compiled SQL statement when performing updates or inserts using the ORM? :)

    Greetings
    Danny


  • HarroHarro
    Accepted Answer
    I can't reproduce this.

            $model = Model_Position::find(2);
            $model->name = null;
            $model->save();

    Results in

    UPDATE `position` SET `name` = null WHERE `id` = 2

    You don't have a TypingObserver or anything else interfering? Are you up to date with the framework?
  • Just checked, the handling of "null" is already present in the code from 2011: https://github.com/fuel/core/blame/1.9/develop/classes/database/connection.php#L571
  • Ohhh Harro you are right -.-

    Bad mistake by me: in my code i used Input::post('bla',null) and the problem here is (of course!) that if its not an unchecked checkbox, the input always exists but if the user did not type in anything, the value is an empty string and because the key inside $_POST exists, Input::post returns the empty string instead of null.

    So all is correct with fuel, the problem sat in front of the screen :)

    Thank you very much for your reply and sorry for this :(

    Greetings and a happy new year!
  • No worries, happy new year to you too!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion