南龙 2012-08-30 13:13:02 3499次浏览 1条回复 0 0 0

这个是模型的关系

/**
* @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(); ?>
  • 回复于 2012-08-30 13:37 举报

    格式:
    '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

您需要登录后才可以回复。登录 | 立即注册