使用Yii自动存储datetime [英] to store datetime automatically using Yii

查看:116
本文介绍了使用Yii自动存储datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Yii学习php,我有一个很大的问题,也许你可以帮助我。



我正在使用一个表单来创建用户。我想将我以表格形式引入的用户的数据存储到数据库(MySQL)中,但是我也必须将日期和时间存储在数据库的datetime字段中。我看到一些扩展名,如CJUIDATEPICKER选择日期,但我不知道任何扩展名选择日期时间。我认为更好的方法是在创建新用户时自动存储datetime。现在我有一个格式的文本框,我必须以格式yyyy-mmmm-dddd hh:mm:ss来介绍日期时间,但是我觉得没有用。



请注意,当我在DB中创建注册表时,有人可以帮助我并向我解释如何存储日期时间?



非常感谢你。现在我使用$ model-> reg_date = new CDbExpression('NOW()');然后在

在actionCreate()控制器中,它的工作原理很好,但我认为在您写入任何内容之前,在用户创建表单的字段中显示此datetime将会更好,并在单击提交时存储表单的所有字段。我试图这样做,但是你可以想象这个字段只显示NOW()



这是我在_form.php中的代码的一部分: p>

 < div class =row> 
<?php $ model-> reg_date = new CDbExpression('NOW()');?>
<?php echo $ form-> labelEx($ model,'reg_date'); ?>
<?php echo $ form-> textField($ model,'reg_date'); ?>
<?php echo $ form-> error($ model,'reg_date'); ?>
< / div>

有人可以帮助我吗?



谢谢你再次!

解决方案

如果您在创建新用户时自动存储datetime,我会使用 CDbExpression

  $ model-> created = new CDbExpression('NOW()'); //YYYY-MM-DD HH:MM:SS

和UNIX_TIMESTAMP(NOW())对于unix时间戳。值得一提的是这些功能是MySQL特定的。



我还想指出, CJuiDatePicker 是与Yii捆绑在一起的小部件。对不起,呕吐。






使用php的日期功能而不是MySQL的NOW()更新答案:

  // UserController.php 
public function actionCreate()
{
$ model = new User;
$ model-> created = date(Y-m-d H:i); //添加了这个
if(isset($ _ POST ['User']))
{
$ model-> attributes = $ _ POST ['User'];
if($ model-> save())
{
$ this-> redirect(array('view','id'=> $ model-> id) );
}
}
$ this-> render('create',array(
'model'=> $ model,
));
}

// _form.php在你的活动窗体内
echo $ form-> labelEx($ model,'created'); //显示它,无论如何你想要的。也许标签?
echo $ form-> hiddenField($ model,'created'); //发送给控制器的值

应该是这样!



如果您使用引导,值得一提的是。不要将隐藏的字段首先放在表单中,因为引导第一个子选择器将会弄乱顶部边距。


I am starting to learn php using Yii, and I have a big question, maybe you can help me.

I am using a form to create users. I want to store the data of the users that I introduce in the form into the DB (MySQL), but I also must store the date and the time in the datetime field of the DB. I saw some extensions like CJUIDATEPICKER to pick the date, but I don't know any extension to pick the datetime. I think that the better way is to store the datetime automatically when I create a new user. Now I have a textbox in the form where I have to introduce the datetime with the format yyyy-mmmm-dddd hh:mm:ss, but I think that it isn't useful.

Please, someone coud help me and explain to me how to store a datetime when I create a register into the DB?

Thank you very much.

EDIT:

Now I use "$model->reg_date=new CDbExpression('NOW()');" inside the actionCreate() controller and it works well but I think that would be better to show this datetime inside the field of the user creation form before you write anything and to store all the fields of the form when you click on submit. I tried to do it but as you can imagine the field only show "NOW()"

This is part of my code of the form in _form.php:

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

Someone could help me?

Thank you again!

解决方案

If you choose to store the datetime automatically when you create a new user, I'd use CDbExpression:

$model->created = new CDbExpression('NOW()'); // "YYYY-MM-DD HH:MM:SS"

And UNIX_TIMESTAMP(NOW()) for a unix timestamp. It should be worth mentioning that those functions are MySQL-specific.

I'd also like to point out that CJuiDatePicker is a widget that comes bundled with Yii. Sorry for the nitpicking.


Updated answer using php's date function instead of MySQL's NOW():

// UserController.php
public function actionCreate()
{
    $model=new User;
    $model->created = date("Y-m-d H:i");  // Added this.
    if(isset($_POST['User']))
    {
        $model->attributes=$_POST['User'];
        if($model->save())
        {
            $this->redirect(array('view','id'=>$model->id));
        }
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

// _form.php  inside your Active Form
echo $form->labelEx($model,'created');   // Display it anyway you'd like. A label perhaps?
echo $form->hiddenField($model, 'created'); // Send back value to the controller on submit

That should be it!

And something worth mentioning if you use bootstrap. Don't place a hidden field first in a form because bootstraps first-child css-selectors will mess up the top margin.

这篇关于使用Yii自动存储datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆