How to check if any of the fields of a model are null or empty?

I have a model, $userModel. I want to check if any of the fields for this model are empty or null.

At the moment I am doing it with a big if statement.

if(!empty($userModel->name) && !empty($userModel->address) ... && !empty($userModel->email))
   // All fields have values.

This way works, but if later I need to add another field to the model, then I need to go back to the if and add another && condition there.

How can I do this in one check?

Is there something like: $userModel::model()->areAllFieldsFilled();

Extra info: The model is already saved in the db, and there is no need for user input. This is just me checking how complete a particular model is, by no means will all these fields be required in the database, only a few. Things like $userModel->bio are usually left null.

I want to avoid checking 5 to 10 fields. I don’t want a giant if that has to be maintained when the model changes.

Source: sql

Leave a Reply