Hi everyone,
I am a newbie to web programming in general, so bear with me.
I have a mysql table which is storing the preferences for a user for my web app, now most of the preferences are just yes and no, so i made my table with lots of tinyints for each preference.
I have referred the book Agile Web Application Development with Yii 1.1 and PHP5, and followed the examples.
So accordingly then in the model, i made each of these preferences constants (const types) using something like this :-
class Pageprefs extends CActiveRecord {
//constants for the preferences
const PREFERENCE_ONE_YES = 1;
const PREFERENCE_ONE_NO = 0;
const PREFERENCE_TWO_YES = 1;
const PREFERENCE_TWO_NO = 0;
const PREFERENCE_THREE_YES = 1;
const PREFERENCE_THREE_NO = 0;
//some more yes,no preferences follow
//auto generated functions from Gii
//etc, etc, etc functions
//after the auto-generated functions, i have defined functions to return the preference options, so that they can be used in the views, showing the text instead of the
//integer values, i.e the tinyint values
public function getPreferenceOneOptions(){
return array(
self::PREFERENCE_ONE_YES=>'Yes',
self::PREFERENCE_ONE_NO=>'No',
);
}
//similar functions follow for all the preferences
//then functions to return text for the preference, i.e Yes and No
public function getPreferenceOneText(){
$preferenceOneOptions = $this->getPreferenceOneOptions();
return isset($preferenceOneOptions[$this->preference_one]) ?
$preferenceOneOptions[$this->preference_one] : "unknown value ({$this->preference_one})";
}
//then again similar functions follow for the rest of the preferences
}
Then in the _view.php i have made a simple call to getPreferenceOneText(), to get the text for the preferences, eg:-
<b><?php echo CHtml::encode($data->getAttributeLabel('preference_one')); ?>:</b>
<?php echo CHtml::encode($data->getPreferenceOneText()); ?>
<br />
<!--
Similarly code for the other preferences
-->
Further modification in the view.php file :-
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'user_id',
array(
'name'=>'preference_one',
'value'=>CHtml::encode($model->getPreferenceOneText())
),//instead of 'preference_one',
array(
'name'=>'preference_two',
'value'=>CHtml::encode($model->getPreferenceTwoText())
),//instead of 'preference_two',
//similar arrays for the rest of the preferences
'message',
),
)); ?>
Now that you know what i want to do, the question that i have is, how can i better organize all the functions for getPreferenceOptions() and getPreferenceTexts() in the model?
I mean instead of having so many functions doing similar things, wouldn’t it be better to have a single function to handle all the preference options and another to handle the preference texts?
I think some form of organization will make the code better, as we have similar values for all the preferences, that being Yes and No.
Would this be better :-
//in the AR model
public function getPreferenencesOptions($preference_id) {
switch($preference_id) {
case 0: return array(
self::PREFERENCE_ONE_YES=>'Yes',
self::PREFERENCE_ONE_NO=>'No',
);
case 1: return array(
self::PREFERENCE_TWO_YES=>'Yes',
self::PREFERENCE_TWO_NO=>'No',
);
//more cases for rest of the preferences
default: //error
}
}
//similarly a function for the getPreferencesTexts using similar switch case
So here we’ll have to pass a preference id, which can be directly done from the views.
But this is not much of an organization, i have just put all the code of the functions into switch-cases. I think a better solution is possible.
Please take some time out to answer my question.