Update record, but only on non-blank fields of form

Hey all, slowly but surely feel like I am getting the hang of Yii, but inevitably another question arises. I am currently working on a profile update page, and currently no fields in the form are required. I would like to update the records for the fields that are filled in but leave the record untouched for the fields that the user leaves blank. I have created other update pages, but this is the first with non-required fields, so I couldn’t simply do a massive assignment like previously. I would imagine that this is a fairly typical scenario, so I am wondering if anybody has come across an elegant solution?

Thanks,

You can do massive assignment on any attribute with a validation rule.

So, define some validation rule for those fields that are not required.

For example "length" might be a good candidate for them. And use "safe" rule as a last resort.

http://www.yiiframework.com/doc/guide/1.1/en/form.model#declaring-validation-rules

Thanks Softark, but I think I may have not explained my question well enough. I know I have to provide validation rules on all fields in the form, but am confused on the massive assignment step. Hopefully I can provide more clarification:

  1. User registers, providing some but not all of the profile information.

  2. User goes to update, where no fields are required but have validation rules of some sort. He will fill in the fields that he likes to update but leaves other fields blank.

  3. Now go to assign, where confusion comes in. Some of the fields may be blank, however I would not like to store an empty string in the table, I would just like to leave it untouched. I could do a bunch of checks for "", but that would leave me with very ugly code.

As my Yii experience so far has taught me, I tend to over think tasks, so I am assuming I am making this harder than it needs to be.

Just loop over the model attributes and check for a non-empty form value for each one:




foreach ($model->attributes as $k=>&$v)

{

  if (!empty($form->$k))

  {

    $v = $form->$k;

  }

}



Ah, OK. I got your point.

So you want to differentiate DB null from empty string in a strict manner.

But does it give you any practical merit? I don’t think so.

Just to keep things simple, I usually treat DB null as empty string. It might not be a good practice in other languages like C/C++, but I think it’s the right choice in PHP. I love the “proper” looseness of PHP.

But I don’t have a good confidence in my opinion. I’d like to hear other voices. :)

Hi softark. Always good to hear your thoughts. I think there’s one exception to the rule: date and datetime fields. I’d better leave them null if the attribute is empty, otherwise I end up with 0000-00-00 in the db, and that is converted by PHP to Jan 1, 1970 as we know. So instead of doing test in afterFind, I’d prefer do it before saving…

Oh! date and datetime!! I hate them. :-[

I really don’t know how to design and code them right in DB and in PHP.