如何用Yii中的数据库值填充文本字段?

时间:2012-03-13 02:26:03

标签: php yii textfield

我想用数据库中的值填充其中一个文本字段。

通常,建议使用此代码:

<div class="row">
  <?php echo $form->labelEx($model,'field1'); ?>
  <?php echo Model1::model()->FindByPk($model->id)->field1;?>
  <?php echo $form->error($model,'field1'); ?>
</div>

但是,我正在搜索的那个值是填写到表单的文本字段中。

有人能帮帮我吗?感谢..

2 个答案:

答案 0 :(得分:1)

试试这个:

<?php echo Chtml::textField('txtFieldName', Model1::model()->FindByPk($model->id)->field1); ?>

答案 1 :(得分:1)

你看过Yii's Blog Demo了吗?它也可以在下载包中找到。

通常,建议这样做:

<?php
echo $form->labelEx($model, 'field', 'label text');
echo $form->textField($model, 'field');
echo $form->error($model, 'field');
?>

如果模型包含数据库中的数据,则会在加载视图时显示该数据。 控制器操作中的代码可能如下所示:

// ...
$model = Model1::model()->findByPk($id); // $id has to be the primary key of the model you want to load
$this->render('viewfile', array(
    'model'=>$model, // this $model is then the same $model as in the view
));