Yii app how to make hidden field for date current

2019-06-08 17:56发布

I created model view and controller for advertise. i want a date in add table as current date. in view generated by gii it displays text input field. how can i make it hidden so that it can get current date without taking input from user?? what should I change? help me please. this is not a complex but i getting confused.. thanks

view/_form.php

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'add-form',
'enableAjaxValidation'=>false,
)); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>

<div class="row">
    <?php echo $form->labelEx($model,'username'); ?>
    <?php echo $form->textField($model,'username',array('size'=>50,'maxlength'=>50)); ?>
    <?php echo $form->error($model,'username'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'addname'); ?>
    <?php echo $form->textField($model,'addname',array('size'=>60,'maxlength'=>100)); ?>
    <?php echo $form->error($model,'addname'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'category'); ?>
    <?php echo $form->textField($model,'category',array('size'=>60,'maxlength'=>100)); ?>
    <?php echo $form->error($model,'category'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'description'); ?>
    <?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>1000)); ?>
    <?php echo $form->error($model,'description'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'city'); ?>
    <?php echo $form->textField($model,'city',array('size'=>50,'maxlength'=>50)); ?>
    <?php echo $form->error($model,'city'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'address'); ?>
    <?php echo $form->textField($model,'address',array('size'=>60,'maxlength'=>500)); ?>
    <?php echo $form->error($model,'address'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'mobile'); ?>
    <?php echo $form->textField($model,'mobile',array('size'=>60,'maxlength'=>100)); ?>
    <?php echo $form->error($model,'mobile'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'email'); ?>
    <?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>100)); ?>
    <?php echo $form->error($model,'email'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'image'); ?>
    <?php echo $form->textField($model,'image',array('size'=>60,'maxlength'=>100)); ?>
    <?php echo $form->error($model,'image'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'price'); ?>
    <?php echo $form->textField($model,'price',array('size'=>60,'maxlength'=>100)); ?>
    <?php echo $form->error($model,'price'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'createtime'); ?>
    <?php echo $form->textField($model,'createtime'); ?>
    <?php echo $form->error($model,'createtime'); ?>
</div>

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

<?php $this->endWidget(); ?>

</div><!-- form -->

标签: php yii
5条回答
乱世女痞
2楼-- · 2019-06-08 18:07

You can simply CURRENT_TIMESTAMP in your database, but unfortunately it only works for 1 row in a table

查看更多
放我归山
3楼-- · 2019-06-08 18:14

Instead of

<?php echo $form->textField($model,'createtime'); ?>

Use

<?php echo $form->hiddenField($model,'createtime'); ?>

Alternatively in your model, you can set default values in afterConstruct():

protected function afterConstruct() {

    parent::afterConstruct();

    if($this->hasAttribute('createtime') && empty($this->createtime))
        $this->createtime = new Date();

Another way to do it is set the default value in the database to CURRENT_TIMESTAMP.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-06-08 18:22

You can do that in Your controller. For example in yuor Controller` create action

 public function actionCreate()
 {
 $model=new Product;
 $model->createtime = strtotime('Now');
 if(isset($_POST['Product']))
 {........}
 .......
 }

And remove from _from.php

 <div class="row">
  <?php echo $form->labelEx($model,'createtime'); ?>
  <?php echo $form->textField($model,'createtime'); ?>
  <?php echo $form->error($model,'createtime'); ?>
 </div>
查看更多
ゆ 、 Hurt°
5楼-- · 2019-06-08 18:30

I think better put assign value on Model. you can use beforeSave() to assign value into 'createtime' before model saved.

on # protected/models/YourModel.php add method like this :

public function beforeSave(){
    if($this->isNewRecord){
        $this->createtime = date('Y-m-d H:i:s');
    }else{
        $this->modifiedtime = date('Y-m-d H:i:s'); 
       //just case if you have modifiedtime field 
    }
    return parent::beforeSave();
} 

after that, just removed this code from view form.

<div class="row">
     <?php echo $form->labelEx($model,'createtime'); ?>
     <?php echo $form->textField($model,'createtime'); ?>
     <?php echo $form->error($model,'createtime'); ?>
</div>
查看更多
叛逆
6楼-- · 2019-06-08 18:31

Use simply:

date('Y-m-d H:i:s')
查看更多
登录 后发表回答