简体   繁体   English

CakePHP:注册用户无法保存正确的日期

[英]CakePHP: Registering user does not save correct date

I have a CakePHP application that allows a user to register and post projects. 我有一个CakePHP应用程序,允许用户注册和发布项目。 At some point whilst I have been tinkering with it I have managed to change the way the current date is captured when the user record is created :( when a new user account is created the date is defaulted to 在某些时候,尽管我一直在修改它,但我设法更改了在创建用户记录时捕获当前日期的方式:(当创建新用户帐户时,该日期默认为

Jan 19th 1970, 01:00

Here is my register action in the users controller: 这是我在用户控制器中的注册动作:

 function register()
{
    if ( !empty( $this->data ) ){
       if ( $this->data['User']['password'] 
                == $this->Auth->password( $this->data['User']['password_confirm'])) {

                    $this->User->create();
                    if ( $this->User->save($this->data) ){
                        $this->redirect(array('action' => 'index')); 
                    }

        } else {
            $this->Session->setFlash('password mismatch');
  }
 }
}

Register view: 注册视图:

<?php
$currentdatetime = time();
echo $form->create( 'User', array('action' => 'register' ) );
echo $form->input('username', array( 'label' => 'Username' ) );
echo $form->input('fname', array( 'label' => 'First name' ) );
echo $form->input('lname', array( 'label' => 'Last name' ) );
echo $form->input('emailaddress', array( 'label' => 'Email Address') );
echo $form->input('website', array( 'label' => 'Website') );
echo $form->hidden( 'created', array( 'value' => $currentdatetime ) );
echo $form->input('password', array( 'label' => 'Password', 'type' => 'password') );
echo $form->input('password_confirm', array( 'label' => 'Retype Password', 'type' => 'password' ) );
 echo $form->submit();
echo $form->end();
?>

I have a users table with a created DATETIME column. 我有一个具有创建的DATETIME列的用户表。 Not NULL, default '0000-00-00' 不为NULL,默认为'0000-00-00'

Can someone help me out here? 有人可以帮我吗? I'm thinking I've maybe just deleted a line of cod or something 我想我可能只是删除了一条鳕鱼线或其他东西

Thanks, 谢谢,

Jonesy Jonesy

The default value for the created column has to be NULL according to the cookbook : 根据菜谱created列的默认值必须为NULL

These fields need to be datetime fields with the default value set to NULL to be recognized by CakePHP. 这些字段必须是日期时间字段,默认值设置为NULL,才能被CakePHP识别。

On inserts, cakePHP will automatically fill in the date if there is a DATETIME column named created . 在插入时,如果有一个名为created的DATETIME列,则cakePHP将自动填写日期。 You don't need to fill in the date yourself. 您不需要自己填写日期。

If you do wish to manually fill in the date, then you should to do it in db format (eg, YYYY-MM-DD hh:mm:ss ). 如果您希望手动填写日期,则应以db格式(例如YYYY-MM-DD hh:mm:ss )进行填写。 Your code is just passing a unix timestamp and that's why the date isn't filled in correctly. 您的代码仅传递了unix时间戳,这就是为什么日期输入不正确的原因。

To submit the date manually, you have couple of options. 要手动提交日期,您有几个选择。 You could use the date function: 您可以使用日期函数:

$currentdatetime = date('Y-m-d H:i:s');

or you could use the database's native function. 或者您可以使用数据库的本机功能。 For MySQL, that would be NOW() : 对于MySQL,那将是NOW()

$this->data['User']['created'] = DboSource::expression('NOW()');
$this->User->save($this->data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM