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 8847407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:11:00+00:00 2026-06-14T12:11:00+00:00

In model: public function getOptionsGender() { array(0=>’Any’, 1=>Male’, 2=>’Female’); } In view (edit): echo

  • 0

In model:

public function getOptionsGender()
{
    array(0=>'Any', 1=>Male', 2=>'Female');
}

In view (edit):

echo $form->dropDownList($model, 'gender', $model->optionsGender);

but I have a CDetailView with “raw” attributes, and it displays numbers instead of genders.

$attributes = array(
    ...
    'gender',
)
  1. What is appropriate way to convert these numbers back to genders? Should I do it in a model, replacing fields such as $this->gender = getOptionsGender($this->gender)? Any github examples will be very appreciated.

  2. I had to choose gender, age, city, country etc. in a few views that are not related to this one. Where should I place my getOptionsGender function definitions?


Thank for your help, the problem is solved.
In model:

public function getGenderOptions() { ... }

public function genderText($key)
{
    $options = $this->getGenderOptions();
    return $options[$key];
}

In view:

$attributes = array(
    array (
        'name'=>'gender',
        'type'=>'raw',
        'value'=>$model->genderText($model->gender), //or $this->genderText(...)
    ),
);

$this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>$attributes,
));

The working example can be found here:
https://github.com/cdcchen/e23passport/blob/c64f50f9395185001d8dd60285b0798098049720/protected/controllers/UserController.php

  • 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-14T12:11:01+00:00Added an answer on June 14, 2026 at 12:11 pm

    In Jeffery Winsett’s book “Agile Web Application Development with Yii 1.1”, he deals with the issue using class constants in the model you are using. In your case:

    class Model extends CActiveRecord
    {
        const GENDER_ANY=0;
        const GENDER_MALE=1;
        const GENDER_FEMALE=2;
    
        public function getGenderOptions(){
            return array(
                self::GENDER_ANY=>'Any',
                self::GENDER_MALE=>'Male',
                self::GENDER_FEMALE=>'Female',
            );
        }
        public function getGenderText(){
            $genderOptions=$this->genderOptions();
            return isset($genderOptions[$this->gender]) ? $genderOptions[$this->gender] : "unkown gender({$this->gender})";
        }
    }
    

    Then in your CDetailView you would have to alter it from gender to:

    array(
        'name'=>'gender',
        'value'=>CHtml::encode($model->genderText()),
    ),
    

    If several models have the same data, you may want to create a base model that extends CActiveRecord and then extend the new model instead of CActiveRecord. If this model is the only one with that data (ie User model only has gender), but other views use that model to display data, then I would leave it just in the single model class. Also keep in mind that if you place getGenderOptions in the extended class, and you extend ALL your models, they will all have that option available, but may not have the attributes needed and will throw an error if you aren’t checking for it.

    All this being said, I still think it is a matter or preference. You can handle it however you want, wherever you want. This is just one example from a book I have specifically on Yii.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I written a function in model: public function plan_values($id) { echo $id; $db =
I have this function write in a CakePHP model: public function getPeopleByName($name){ $this->unbindModel(array('hasMany' =>
I have a validation rules in my model: public function rules() { return array(){
I have associative array: Model public function selectArticle(){ $sql = $this->db->query(SELECT * FROM articles);
model public function cart() { $this->load->model('Cart_model'); $this->load->view('head'); $data['pricing'] = $this->Cart_model->R_products(); $this->load->view('cart', $data); $this->load->view('footer'); }
I have this query in model: public function Games() { $q = $this->db->select('games.id, games.title,
I have the following piece of code in my Model: public function getSite() {
I have a problem with CI. I have a model: public function Game($id) {
i created a model object in PHP class User { public $title; public function
Here is my model public function getDetails($ip) { $this->db->select('country_code, country_name'); $this->db->where('2323' BETWEEN begin_ip_num AND

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.