表单中的更新值不会显示在视图中

时间:2017-09-06 14:04:41

标签: php yii2 yii2-advanced-app yii2-basic-app

我在PHP开发中使用Yii2作为我的框架,在我的ticket.php中,当time.php中的特定数据将被更新时,“time_end”将自动获取计算机时间我的问题是它何时更新它没有显示在视图上。它说(未设置)

我的TicketController.php

 public function actionCreate()
{
    $model = new Ticket();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        $model->time_start = date('d-m-y h:i:s');
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

/**
 * Updates an existing Ticket model.
 * If update is successful, the browser will be redirected to the 'view' page.
 * @param integer $id
 * @return mixed
 */
public function actionUpdate($id)
{
    $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
            $model->time_end = date('d-m-y h:i:s');            
            return $this->render('update', [
            'model' => $model,
        ]);

    }
}

我的_form.php

<?= $form->field($model, 'time_end')->widget(DateTimePicker::className(),
                                    [
                                        'value' => date('d-m-y h:i:s'),
                                        'disabled' => true
                                    ]


); ?> 

view.php

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model app\models\Ticket */

$this->title = $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Tickets', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="ticket-view">

<h1><?= Html::encode($this->title) ?></h1>

<p>
    <?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
    <?= Html::a('Delete', ['delete', 'id' => $model->id], [
        'class' => 'btn btn-danger',
        'data' => [
            'confirm' => 'Are you sure you want to delete this item?',
            'method' => 'post',
        ],
    ]) ?>
</p>

1 个答案:

答案 0 :(得分:0)

在规则中将time_end字段添加到safe。

e.g

public function rules()
{
    return array(
        array('username, password, salt, email', 'required'),
        array('username, password, salt, email', 'length', 'max'=>128),
        array('profile', 'safe'),
    );
}