Yii Framework Forum: Cant Gii Generate Dropdown In Place Of Textfield? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cant Gii Generate Dropdown In Place Of Textfield? Rate Topic: ***** 1 Votes

#1 User is offline   Ravi Bhalodiya 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 27-August 12
  • Location:Gujarat India

Posted 06 November 2012 - 04:29 AM

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????
Regards
Ravi Bhalodiya.
2

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 06 November 2012 - 06:48 AM

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
1

#3 User is offline   xahgmah 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 08-October 12
  • Location:Ukraine, Kharkiv

Posted 06 November 2012 - 06:58 AM

View PostRavi Bhalodiya, on 06 November 2012 - 04:29 AM, said:

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'))";
		}		
	}

1

#4 User is offline   Ravi Bhalodiya 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 27-August 12
  • Location:Gujarat India

Posted 06 November 2012 - 11:36 PM

I tried it. But it throws following exception.
CrudCode and its behaviors do not have a method or closure named "getRelationClassname".
Regards
Ravi Bhalodiya.
0

#5 User is offline   xahgmah 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 08-October 12
  • Location:Ukraine, Kharkiv

Posted 08 November 2012 - 05:06 AM

View PostRavi Bhalodiya, on 06 November 2012 - 11:36 PM, said:

I tried it. But it throws following exception.
CrudCode and its behaviors do not have a method or closure named "getRelationClassname".


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;
	}

1

#6 User is offline   Ravi Bhalodiya 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 27-August 12
  • Location:Gujarat India

Posted 19 November 2012 - 03:37 AM

I tried this... But it still generates textFields. I dont know the problem. Is there any changes required in database side???
Regards
Ravi Bhalodiya.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users