Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Model Propery Default Value
  • Hi, I've seen this issue posted here on github: https://github.com/fuel/orm/issues/12 and was wondering whether it was actually implemented into the public branch as mentioned in the second comment? I've been trying to set it up as I don't want my field to be stored with a null value, want either 0 or 1! Below is the code i've used in the top of my model but doesn't seem to accept the value and still tries to enter a null value when my checkboxes haven't ticked.
     protected static $_properties = array(
      'id', 
      'username' => array(
       'validation' => array('required')
      ), 
      'password' => array(
       'validation' => array('required')
      ),
      'email' => array(
       'validation' => array('required', 'valid_email')
      ),
      'active' => array(
       'default' => 0
      )
    };
    

    Anyone else come across this?
  • Moved to the ORM forum.
  • It has been part of the ORM as of at least the 1.1 release, maybe even 1.0.1 - can't remember.
  • Oh, is there anything I'm doing wrong then? As I say it's still inserting a null value rather then the default. Here is the line I have for the checkbox
    echo Form::checkbox('data[active]', 1);
    

    And here's the lines from the controller saving the model.
    $user = Model_User::forge();
    $user->username = Input::post('data.username');
    $user->password = sha1(Input::post('data.password'));
    $user->email = Input::post('data.email');
    $user->active = Input::post('data.active');
        
    $user->profile = Model_Profile::forge();
    $user->profile->profile = Input::post('data.profile');
        
    $user->save();
    

    And above is my declaration of the properties array in the User model. From my understanding of the docs, that should all work or have I missed something out? I've looked over and over again at the docs but can't seem to find anything that I've done potentially wrong. Cheers EDIT:: FYI I am running fuel 1.1 that I downloaded from the main button on the homepage saying "Download v1.1"
  • If you'd check $user->active after creating the $user instance with Model_User::forge() you would get 0. However you overwrite the value using $user->active = Input::post('data.active') after which it will be either 1 or null. Because Input::post('data.active') either returns the value of the checkbox when checked or won't exist in the $_POST array and return null. The default values are there if you don't overwrite them, you quite clearly intentionally overwrite them thus I'm not sure why you'd expect there to still be another value than the return of Input::post(). How to solve this, the next options will work equally well - which is best is up to you.
    // make the current value of $user->active the 'default' return value of Input::post() when the requested key isn't found
    $user->active = Input::post('data.active', $user->active);
    
    // Similar to the above but default to zero
    $user->active = Input::post('data.active', 0);
    
    // Only set when input is found
    Input::post('data.active') and $user->active = Input::post('data.active');
    
  • Ahhhh, my mistake I misunderstood how I thought it worked. I thought the Input::post() would return the default value if set but as I'm saying this I realise Input::post wouldn't know which property it was actually linked to.
    My bad, thanks for your help!

Howdy, Stranger!

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

In this Discussion