Updated value in form doesn't show on view

72 Views Asked by At

I'm using Yii2 as my framework in PHP develiping, In my ticket.php the "time_end" will automatically get the computer time when a specific data in ticket.php is going to be updated my problem is when it gets updated it doesn't show on view. It says (not-set)

My 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,
        ]);

    }
}

My _form.php

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


); ?> 

The 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

There are 1 best solutions below

0
On

Add time_end field to safe in the rules.

e.g

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