我有一个类别,我需要创建一个子类别。
当我尝试创建子类别时,我有一个名为category Name的字段,该字段应预先加载我想要创建子类别的类别名称。
我怎样才能实现这一目标..
我的控制器
public function actionCreate($id)
{
$model=new SubCategory;
$mode = Category::model()->findAll();
$mode_1=$this->loadModel1($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['SubCategory']))
{
$model->attributes=$_POST['SubCategory'];
if($model->save())
$this->redirect(array('create','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
表格
<?php
/* @var $this SubCategoryController */
/* @var $model SubCategory */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'sub-category-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<?php
$category_id = $_GET['id'];
$category = Category::model()->findByAttributes(array('id' => $category_id));
// echo '<pre>';print_r($category);'</pre>';
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'category_id'); ?>
<?php echo $form->textField($model,'category_id',
array('class'=>'form-control','style'=>'width:300px;')); ?>
<?php echo $form->error($model,'category_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'subcategory_name'); ?>
<?php echo $form->textField($model,'subcategory_name',array('class'=>'form-control','style'=>'width:300px;')); ?>
<?php echo $form->error($model,'subcategory_name'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array('class' => 'btn btn-default')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<div style="width: 97%; margin: auto;" >
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'fav-category-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
// 'id',
//'favcategory',
array('header'=>'#','htmlOptions'=>array('width'=>'60px'),
'value'=>'$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),
//'id',
//'category_id',
array(
'name' => 'category_id',
'value' => '$data->category->categoryname',
//'filter'=> CHtml::listData(Category::model()->findAll(array('order'=>'categoryname')), 'categoryid', 'categoryname')
),
'subcategory_name',
array(
'class' => 'CButtonColumn',
'htmlOptions'=>array('width'=>'110px'),
'template' => '{update}{delete}',
'buttons' => array(
'update'=>array(
'imageUrl' =>false,
'label' => '',
'options' => array('title'=>'update','class'=>'btn btn-info btn-xs fa fa-pencil-square-o'),
),
'delete'=>array(
'imageUrl' =>false,
'label' => 'delete',
'options' => array('title'=>'view','class'=>'btn btn-danger btn-xs'),
),
)
),
),
'itemsCssClass'=>'table table-striped table-bordered table-hover',
'pagerCssClass'=>'pagination',
'pager'=>array( 'header' => '','lastPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span><span class="glyphicon glyphicon-chevron-right"></span>','firstPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span><span class="glyphicon glyphicon-chevron-left"></span>','prevPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span>','nextPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span>','header' => '','cssFile' => Yii::app()->baseUrl . '/css/pager.css','htmlOptions'=>array('class'=>'pagination'),'selectedPageCssClass'=>'active'),
)); ?>
</div>
答案 0 :(得分:0)
我可以看到类型模型$category
被加载到表单中。
$category = Category::model()->findByAttributes(array('id' => $category_id));
如果id
表中的category
是主键,那么我建议,为了简单起见:
$category = Category::model()->findByPk($category_id);
现在我猜你可能想要在textField中禁用它。
因此,请在表单中使用textField()
CActiveForm
,例如:
$form->textField($category, 'name', array('disabled' => 'disabled'));
textField docs
注意:第二个参数必须是属性名称,我假设为name
。
此外,我在actionCreate()
中未看到使用以下两行。我的意思是,变量$mode
和$mode_1
不会在该操作中的任何位置使用。
$mode = Category::model()->findAll();
$mode_1=$this->loadModel1($id);