Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Default integer value in ORM
  • I have an ORM model I created with scaffolding and one of the fields is INT NOT NULL DEFAULT:0 I have also removed the 'required' validator on the field. I was hoping that if I did not pass a value from the form it would not pass anything to the database when creating the record, and the database would apply the default value of 0. Unfortunately this doesnt work and when the save method is called I get a Fuel\Core\Database_Exception which states 'Invalid intege format:'. When I debug the method I can see that in the insert statement that it generates, it is trying to insert the field with an empty string (''). Does anybody know an easy way to avoid this? For now I have made some changes to the action_create method on the controller to set the value of the field to 0 when forging the model, if a value is not passed from the form. I could also amend the form but I dont really want to put all the logic up in the UI. Thx. Neil.
  • You will have to define the default on the $_properties array. The ORM doesn't read back defaults from the table definition.
  • Ah, I had missed that in the docs. However I have tried it but still have the same problem. In the ORM I have tried.... protected static $_properties = array(
    'id',
    'my_int_val' => array('default' => 0), ...and I have also tried using a default value in the Input static in the controller... $foo = Model_Foo::forge(array(
    'my_int_val' => Input::post('my_int_val', 0), ...both to no avail. It still seems like an empty string '' is passed in the SQL. The only way I have managed to get round it so far is to perform a check prior to assigning the values in the create method of the controller.... $my_int_val = (is_numeric( Input::post('my_int_val')) ? Input::post('my_int_val') : 0); ...and then assigning this into the model. It works but its alot of fiddling for every optional field and takes away some of the benefit of using the scaffolding to create the basic actions. Am I still missing something???? Thx
  • Which version of FuelPHP are you using? If you're on 1.1, you might want to switch to 1.2RC1, I remember some issues with default values a few months ago.
  • Swapped to 1.2 but still no good. What I have found is that if I do not set the field at all when I forge the Model, then the default value specified in the model seems to work, but as soon as I have set the value, even if it is not populated when submitting the form, it will fail. This works, with the setting of the int val commented out $foo = Model_Foo::forge(array(
    //'my_int_value' => Input::post('my_int_value'),
    This doesnt $foo = Model_Foo::forge(array(
    'my_int_value' => Input::post('my_int_value'),
    Here is a snippet of a var_dump directly before I call ->save in the controller object(Model_Foo)#41 (9) {
    ["_is_new":protected]=>
    bool(true)
    ["_frozen":protected]=>
    bool(false)
    ["_data":protected]=>
    array(18) {
    ["my_int_value"]=>
    string(0) ""
    ...
    ... You can see my_in_value has been set as an empty string. Is this the problem? Does the ORM see this as a valid value and use this as opposed to the default??? Where exactly in the ORM does it decide whether or not to use the default values?? If you can point me in the right direction I may be able to debug it to see why its not doing as expected. Thanks for your help so far...
  • Pretty logical. You set a value, therefore a default no longer applies. Input::post() always returns a value, NULL is a value (especially in a database context, where NULL can be a valid value for a column!). So if you don't want that, check before you assign.
  • Got there in the end. That was basically the original question, can I just use the scaffolding 'out of the box' in the specific scenario which is obviosuly what we all strive for, or do I nned to tweak it. And the answer is that I need to tweak it. Thx.

Howdy, Stranger!

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

In this Discussion