表单中的关联模型,求解? [ 新手入门 ]
这个是模型的关系
/**
* @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(
'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
'author' => array(self::BELONGS_TO, 'User', 'author_id'),
'article_data' => array(self::HAS_ONE, 'ArticleData', 'article_id'),
);
}
这个是表单
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
'id'=>'article-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="help-block">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->textFieldRow($model,'title',array('class'=>'span5','maxlength'=>400)); ?>
<?php echo $form->textAreaRow($model,'description',array('rows'=>3, 'cols'=>50, 'class'=>'span8')); ?>
//下面这行的问题.为什么不能这样用呢?有没有更好的方法?
<?php echo $form->textAreaRow($model->article_data,'content',array('rows'=>6, 'cols'=>50, 'class'=>'span8')); ?>
<?php echo $form->textFieldRow($model,'tags',array('class'=>'span5','maxlength'=>120)); ?>
<?php echo $form->DropDownListRow($model,'category_id',CHtml::listData(Category::model()->findAll(),'id','name'),array('empty'=>'分类选择','class'=>'span5')); ?>
<?php echo $form->DropDownListRow($model,'status',$model->getStatus(),array('class'=>'span5')); ?>
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>$model->isNewRecord ? 'Create' : 'Save',
)); ?>
</div>
<?php $this->endWidget(); ?>
共 1 条回复
-
格式:
'VarName'=>array('RelationType', 'ClassName', 'ForeignKey', ...additional options)
个人理解:
(1)VarName代表的是一个对象,例如通过外键author_id(ForeignKey)获取到author(VarName)对象。
(2)RelationType。一共有4种,分别为self::HAS_MANY, self::BELONGS_TO, self::MANY_MANY, self::HAS_ONE。
(3)ClassName。即关联的另一个../model/类名.php,例如User模型。相当于JOIN后跟的表名。
(4)要关联的另外那个表的外键ForeignKey。
(5)附加条件,相当于on里面的语句。请看文档:http://www.yiichina.com/doc/api/1.1/CActiveRecord#relations-detail
南龙
注册时间:2012-01-05
最后登录:1970-01-01
在线时长:0小时0分
最后登录:1970-01-01
在线时长:0小时0分
- 粉丝0
- 金钱135
- 威望0
- 积分135