0

Yii 2 Docs explains that I can set the fields that should be returned by default by toArray(). (http://www.yiiframework.com/doc-2.0/yii-base-model.html#fields()-detail)

Theres any possibility to ignore when contains null values?

function fields() {
    return [
        'email', // Ignore if email is null.
        'fullName', // Ignore if fullName is null.
    ];
}

1 Answer 1

1

Try this:

function field() {
    $return = [];
    if(!empty($this->email)) {
        $return[] = 'email';
    }
    if(!empty($this->fullName)) {
        $return[] = 'fullName';
    }
    return $return;
}
Sign up to request clarification or add additional context in comments.

Comments

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.