clientScript->registerScript('gii.model',"
$('#{$class}_connectionId').change(function(){
var tableName=$('#{$class}_tableName');
tableName.autocomplete('option', 'source', []);
$.ajax({
url: '".Yii::app()->getUrlManager()->createUrl('gii/model/getTableNames')."',
data: {db: this.value},
dataType: 'json'
}).done(function(data){
tableName.autocomplete('option', 'source', data);
});
});
$('#{$class}_modelClass').change(function(){
$(this).data('changed',$(this).val()!='');
});
$('#{$class}_tableName').bind('keyup change', function(){
var model=$('#{$class}_modelClass');
var tableName=$(this).val();
if(tableName.substring(tableName.length-1)!='*') {
$('.form .row.model-class').show();
}
else {
$('#{$class}_modelClass').val('');
$('.form .row.model-class').hide();
}
if(!model.data('changed')) {
var i=tableName.lastIndexOf('.');
if(i>=0)
tableName=tableName.substring(i+1);
var tablePrefix=$('#{$class}_tablePrefix').val();
if(tablePrefix!='' && tableName.indexOf(tablePrefix)==0)
tableName=tableName.substring(tablePrefix.length);
var modelClass='';
$.each(tableName.split('_'), function() {
if(this.length>0)
modelClass+=this.substring(0,1).toUpperCase()+this.substring(1);
});
model.val(modelClass);
}
});
$('.form .row.model-class').toggle($('#{$class}_tableName').val().substring($('#{$class}_tableName').val().length-1)!='*');
");
?>
Model Generator
This generator generates a model class for the specified database table.
beginWidget('CCodeForm', array('model'=>$model)); ?>
labelEx($model, 'connectionId')?>
textField($model, 'connectionId', array('size'=>65))?>
The database component that should be used.
error($model,'connectionId'); ?>
labelEx($model,'tablePrefix'); ?>
textField($model,'tablePrefix', array('size'=>65)); ?>
This refers to the prefix name that is shared by all database tables.
Setting this property mainly affects how model classes are named based on
the table names. For example, a table prefix tbl_
with a table name tbl_post
will generate a model class named Post
.
Leave this field empty if your database tables do not use common prefix.
error($model,'tablePrefix'); ?>
labelEx($model,'tableName'); ?>
widget('zii.widgets.jui.CJuiAutoComplete',array(
'model'=>$model,
'attribute'=>'tableName',
'name'=>'tableName',
'source'=>Yii::app()->hasComponent($model->connectionId) ? array_keys(Yii::app()->{$model->connectionId}->schema->getTables()) : array(),
'options'=>array(
'minLength'=>'0',
'focus'=>new CJavaScriptExpression('function(event,ui) {
$("#'.CHtml::activeId($model,'tableName').'").val(ui.item.label).change();
return false;
}')
),
'htmlOptions'=>array(
'id'=>CHtml::activeId($model,'tableName'),
'size'=>'65',
'data-tooltip'=>'#tableName-tooltip'
),
)); ?>
This refers to the table name that a new model class should be generated for
(e.g. tbl_user
). It can contain schema name, if needed (e.g. public.tbl_post
).
You may also enter *
(or schemaName.*
for a particular DB schema)
to generate a model class for EVERY table.
error($model,'tableName'); ?>
label($model,'modelClass',array('required'=>true)); ?>
textField($model,'modelClass', array('size'=>65)); ?>
This is the name of the model class to be generated (e.g. Post
, Comment
).
It is case-sensitive.
error($model,'modelClass'); ?>
labelEx($model,'baseClass'); ?>
textField($model,'baseClass',array('size'=>65)); ?>
This is the class that the new model class will extend from.
Please make sure the class exists and can be autoloaded.
error($model,'baseClass'); ?>
labelEx($model,'modelPath'); ?>
textField($model,'modelPath', array('size'=>65)); ?>
This refers to the directory that the new model class file should be generated under.
It should be specified in the form of a path alias, for example, application.models
.
error($model,'modelPath'); ?>
labelEx($model,'buildRelations'); ?>
checkBox($model,'buildRelations'); ?>
Whether relations should be generated for the model class.
In order to generate relations, full scan of the whole database is needed.
You should disable this option if your database contains too many tables.
error($model,'buildRelations'); ?>
labelEx($model,'commentsAsLabels'); ?>
checkBox($model,'commentsAsLabels'); ?>
Whether comments specified for the table columns should be used as the new model's attribute labels.
In case your RDBMS doesn't support feature of commenting columns or column comment wasn't set,
column name would be used as the attribute name base.
error($model,'commentsAsLabels'); ?>
endWidget(); ?>