Gii Auto Generated Code For Specific Fields

I am using gii to auto generate crud function. But i do not want it for all the fields. I want it to work for specific fields:

for example

user_id(auto increment)

firstName

lastName

email

validate

now i dot want to put value using form in user_id and validate field. I do not want to view these fields in the form. Because these are either auto generated or operational fields. And general users does not need to know about it. So, like how to do this using gii.

Hi Ahmmad,

Just remove the unnecessary fields from the gii-generated form by hand. :)

thank you very much.

like remove from model or controller. can you give me an example:

like i have the following model for user:

<?php

/**

  • This is the model class for table "{{users}}".

  • The followings are the available columns in table ‘{{users}}’:

  • @property integer $user_id

  • @property integer $user_type

  • @property string $user_name

  • @property string $firstName

  • @property string $lastName

  • @property string $email

  • @property string $user_registered

  • @property string $lastVisitDate

  • @property boolean $activation

  • @property boolean $block

*/

class Users extends CActiveRecord

{

/**


 * Returns the static model of the specified AR class.


 * @param string &#036;className active record class name.


 * @return Users the static model class


 */


public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}





/**


 * @return string the associated database table name


 */


public function tableName()


{


	return '{{users}}';


}





/**


 * @return array validation rules for model attributes.


 */


public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('user_type', 'numerical', 'integerOnly'=&gt;true),


		array('user_name, firstName, lastName, email', 'length', 'max'=&gt;50),


		array('user_registered, lastVisitDate, activation, block', 'safe'),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('user_id, user_type, user_name, firstName, lastName, email, user_registered, lastVisitDate, activation, block', 'safe', 'on'=&gt;'search'),


	);


}





/**


 * @return array relational rules.


 */


public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'user_id' =&gt; 'User',


		'user_type' =&gt; 'User Type',


		'user_name' =&gt; 'User Name',


		'firstName' =&gt; 'First Name',


		'lastName' =&gt; 'Last Name',


		'email' =&gt; 'Email',


		'user_registered' =&gt; 'User Registered',


		'lastVisitDate' =&gt; 'Last Visit Date',


		'activation' =&gt; 'Activation',


		'block' =&gt; 'Block',


	);


}





/**


 * Retrieves a list of models based on the current search/filter conditions.


 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.


 */


public function search()


{


	// Warning: Please modify the following code to remove attributes that


	// should not be searched.





	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('user_id',&#036;this-&gt;user_id);


	&#036;criteria-&gt;compare('user_type',&#036;this-&gt;user_type);


	&#036;criteria-&gt;compare('user_name',&#036;this-&gt;user_name,true);


	&#036;criteria-&gt;compare('firstName',&#036;this-&gt;firstName,true);


	&#036;criteria-&gt;compare('lastName',&#036;this-&gt;lastName,true);


	&#036;criteria-&gt;compare('email',&#036;this-&gt;email,true);


	&#036;criteria-&gt;compare('user_registered',&#036;this-&gt;user_registered,true);


	&#036;criteria-&gt;compare('lastVisitDate',&#036;this-&gt;lastVisitDate,true);


	&#036;criteria-&gt;compare('activation',&#036;this-&gt;activation);


	&#036;criteria-&gt;compare('block',&#036;this-&gt;block);





	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


	));


}

}

i want to remove

  • @property string $user_registered

  • @property string $lastVisitDate

  • @property boolean $activation

  • @property boolean $block

from the crud functionality. Shall i make change in the model page then generate the crud function. If so then please change this (remove the lines) code so that i can understand and apply afterward.

Thanks

Ah, sorry for the confusion.

There’s no way to enforce Gii CRUD generator to skip the unnecessary attributes when creating form. It will create an input field for each and every attribute. Gii can not tell what attributes you will need for specific form.

Just delete the unnecessary lines (e.g. user_id, validate, user_registered, … etc) from the generated "_form.php" file.

Usually you don’t have to modify much in the model file. The only parts that needs to be modified are rules() and search(). For the rest of the model file, you’d be better not to remove anything including the doc comments like




* @property string $user_registered

* @property string $lastVisitDate

* @property boolean $activation

* @property boolean $block



thank you very much. it is working nicely.