Ravi005
(Bhalodiyaravi)
November 6, 2012, 9:29am
1
I am new in Yii. I have two tables. info and gender.
info table contain three fields : id, name, gender_id.
gender table contain two fields : id, name(‘Male’,‘Female’)
gender_id is foreign key of gender table.
Now my question is, can gii generate dropDown for the field gender_id of info table when i generate crud for info table?
abennouna
(Abennouna)
November 6, 2012, 11:48am
2
Gii doesn’t do it (yet?), but there are several extensions that do it for you, although it’s quite easy to code manually if a handful of dropdowns are needed. The one I personally use is giix
xahgmah
(Xahgmah)
November 6, 2012, 11:58am
3
I am new in Yii. I have two tables. info and gender.
info table contain three fields : id, name, gender_id.
gender table contain two fields : id, name(‘Male’,‘Female’)
gender_id is foreign key of gender table.
Now my question is, can gii generate dropDown for the field gender_id of info table when i generate crud for info table?
Yes. You mast override the method "generateInputField" from CrudCode class
Here my example
public function generateInputField($modelClass, $column) {
$relationModel = FALSE;
if($column->isForeignKey) {
$relationModel = $this->getRelationClassname($modelClass, $column->name);
}
$dbType = $column->dbType;
preg_match('/([a-z]+)/i',$column->dbType, $matches);
if(count($matches) > 0) {
$dbType = $matches[0];
}
if(($size=$maxLength=$column->size)>60)
$size=60;
switch ($dbType) {
case 'tinyint':
return "CHtml::activeDropDownList(\$model, '{$column->name}', array())";
break;
case 'int':
if($relationModel !== FALSE) {
return "CHtml::activeDropDownList(\$model, '{$column->name}', CHtml::listData({$relationModel}::model()->findAll(), '', ''))";
} else {
return "CHtml::activeTextField(\$model,'{$column->name}',array('size'=>$size, 'maxlength'=>$maxLength, 'class'=> 'text small'))";
}
break;
case 'text':
return "CHtml::activeTextArea(\$model,'{$column->name}',array('rows'=>6, 'cols'=>50))";
break;
case 'datetime':
return "Yii::app()->dateFormatter->formatDateTime(\$model->{$column->name})";
break;
case 'varchar':
default:
if(preg_match('/^(password|pass|passwd|passcode)$/i',$column->name))
$inputField='activePasswordField';
else
$inputField='activeTextField';
return "CHtml::{$inputField}(\$model,'{$column->name}',array('size'=>$size, 'maxlength'=>$maxLength, 'class'=> 'text medium'))";
}
}
Ravi005
(Bhalodiyaravi)
November 7, 2012, 4:36am
4
I tried it. But it throws following exception.
CrudCode and its behaviors do not have a method or closure named "getRelationClassname".
xahgmah
(Xahgmah)
November 8, 2012, 10:06am
5
Sory, i override this class. And add new method getRelationClassname on it too.
public function getRelationClassname($modelClass, $column) {
$model = new $modelClass;
$relations = $model->relations();
foreach($relations as $relation) {
if(isset($relation[2]) && $relation[2] == $column) {
return $relation[1];
}
}
return FALSE;
}
Ravi005
(Bhalodiyaravi)
November 19, 2012, 8:37am
6
I tried this… But it still generates textFields. I dont know the problem. Is there any changes required in database side???