0

whenever I get a User from the database, I want to say "if this field for this user is empty..."

This is for the current user and every user that is retrieved from the database. Where would I put this if statement? Would it go in the model or is there a generic function that I can use the in the user controller before rendering any user?

Many thanks

1 Answer 1

2

You can use afterFind in your model to perform such checks, and this will work for data returned for find operation, like:

//like for User model's some field
public function afterFind($results, $primary = false) {
    foreach ($results as $key => $val) {
        if (empty($val['User']['some_field'])) {
            $results[$key]['User']['some_field'] = "Empty Field"; 
        }
    }
    return $results;
}

Note:- You can define such functions in your Model Class or AppModel as well.

Sign up to request clarification or add additional context in comments.

1 Comment

This is probably not a good idea as it will set the fields to automatically contain this data on your forms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.