views:

24

answers:

2

Hi,

I have a CakePHP application that allows a user to register and post projects. 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. Not NULL, default '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

+2  A: 

On inserts, cakePHP will automatically fill in the date if there is a DATETIME column named created. 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 (e.g., 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.

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():

$this->data['User']['created'] = DboSource::expression('NOW()');
$this->User->save($this->data);
webbiedave
that's interesting because I didn't have tht hidden field there at first and it;s still behaving the same. Tried $this->data['User']['created'] = DboSource::expression('NOW()'); but still I'm getting Jan 19th 1970, 01:00 for the date
iamjonesy
+3  A: 

The default value for the created column has to be NULL according to the cookbook:

These fields need to be datetime fields with the default value set to NULL to be recognized by CakePHP.

dhofstet
Legend! thanks dhofstet!
iamjonesy