0

I've got a default value defined in my model for a field

'limit' => array(AttributeType::Number, 'default' => 0)

When I create a new record, if the limit field is empty, NULL gets stored in the DB instead of 0. I thought $model->setAttributes() would set the default values if the values coming in were NULL. What am I missing?

4
  • Did you define the default value for the attribute in your record as well? – Steve Rowling Apr 3 '16 at 12:30
  • Yes, it's on the record as well. – Trevor Davis Apr 3 '16 at 14:10
  • Does the limit column in the database have a default set? Did the record create the table? – Brad Bell Apr 3 '16 at 22:08
  • Ahhh nope because this column was added in a DB migration. How can I set the default value in a migration? – Trevor Davis Apr 4 '16 at 13:36
0

If you want to add a default to an existing column in a table in a migration, you'd do something like this:

$this->alterColumn('tableName', 'columnName', array('column' => ColumnType::Number, 'default' => 0));

If you want to add a default to a new column in a table in a migration, you'd do something like this:

$this->addColumn('tableName', 'columnName', array('column' => ColumnType::Number, 'default' => 0));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.