Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8837957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:51:51+00:00 2026-06-14T09:51:51+00:00

In my Yii models for some properties I would like to return some default

  • 0

In my Yii models for some properties I would like to return some default when the real property is null or empty.

For example say I have a display_name property. When it hasn’t been set, I’d like to return the first_name.

So far the work around I’ve been using is to create a new method with a different name and call this one instead. However it’d be nice if I could achieve this by simply calling $model->display_name the normal way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:51:53+00:00Added an answer on June 14, 2026 at 9:51 am

    I may have found my own answer. I am overriding __get() to check if this is the property I am looking for. I am not sure this is considered “best practice”.

    public function __get($name) {
                $current = parent::__get($name);
                switch($name){
                    case 'display_name':
                        if(empty($current)){
                            return $this->first_name;
                        }
                        else{
                            return $current;
                        }
                        break;
                    default:
                        return $current;
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models that I would like to present in a single Yii
We have an Yii-based PHP application which does some generic actions (like Share with
Suppose in Yii Framework I have the attributes for the model like this field_1
I'm fairly new to Yii and have run into a barrier with related models.
So I'm getting a bit lost with CActiveRecord in Yii. Let's say I have
I'm starting with yii and I have some trouble when trying to filter an
Update: Ok some update, finally we have decided on the framework (Yii) and have
I need to validate some variables values in Yii; I dont have a model
I'm having nice first days using Yii but have some troubles with getting things
I have a Yii controller action that can potentially return a very large set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.