is it possible to use array instead of model in relation

I had declare a params in the config file




'params'=>array(

 		'gender'=>array(

          		array('id'=>'M', 'name'=>'Male'),

          		array('id'=>'F', 'name'=>'Female'),

		),

),



then in my user model, I have a property call gender, is it possible for me to create a gender relation with the params I created in config file? or is there any other way can do that because I dont want to create another table just have 2 records




public function relations()

{

		return array(

          		'r_gender'=>array(self::BELONGS_TO, Yii::app()->params['gender'], 'gender'),

		);

}



Why don’t you declare constants for the genders?

If you want the gender labels to be configurable, then you can do something like the following:




// config

'params'=>array(

    'male' => 'Male',

    'female' => 'Female',

),


// model class

const MALE = 1;

const FEMALE = 2;


public function getGenderLabel()

{

    return ($this->gender === self::MALE)

        ? Yii::app()->params['male'] :

        : Yii::app()->params['female'] :

}



Because I would like to have a statistic on the gender count.




public function relations()

{

        return array(

                'r_gender'=>array(self::BELONGS_TO, Yii::app()->params['gender'], 'gender'),

                'r_gender_males'=>array(self::STAT, Yii::app()->params['gender'], 'gender', ...),

        );

}

A BELONGS_TO relation can’t be used as a STAT relation.

And, there’s no way to construct a relation between a db table and a PHP array.

What about the other way round? Creating a Gender Model with 2 records. and the User Model has the gender property refer to Gender Model

Gender Model




public function relations()

{

      return array(

            'r_male_count'=>array(self::STAT, 'User', 'gender', 'condition'=>'id=:id', 'params'=>array(':id'=>'M'),

      );

}



I would not create “Gender Model with 2 records”. I don’t see any good reason to create such a model.

[s]I don’t understand what you are trying to do with the STAT relation. Probably you have a wrong idea about it.

A user is either a male or a female. He or she can’t have a “male_count” or a “female_count”.

If you are thinking of a group of users … a group has many users, and a user belongs to a group … then you can think of a STAT relation on the group’s side, not on the user’s side.[/s]

[EDIT]

Ah, so you want to count the number of male and female in the users.

OK. Just count them. That’s all.




// User model

public static function getMaleCount()

{

    return User::model()->count('gender = 1');

}

// or something like this.



ok. thanks a lot softark.