How is current and modified time is saving in CakePHP's blog tutorial?

197 views Asked by At

I have just started learning CakePHP and I started with the official blog tutorial. So far so good but I am not getting how current and modified time is saving in db because neither in views nor in controller there is any line which points to these fields.

Here is PostsController.php code for add:

public function add()
    {
        if($this->request->is('post'))
        {
            $this->Post->create();
            if($this->Post->save($this->request->data))
            {
                $this->Session->setFlash(__('Your post has been saved.'));
                return $this->redirect(array('action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('Unable to add your post.'));
            }
        }
    }

And here is view code for add.ctp:

<!-- File: /app/View/Posts/add.ctp -->
<?php echo $this->Html->link('Home', 'index/home'); ?>
<h1>Add Posts</h1>
<?php
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->submit('Add Post', array('name' => 'add_post'));
//echo $this->Form->end('Save Post');
?>

So someone please tell me which line is saving current time? Thanks a lot for help.

2

There are 2 answers

1
Ben Hitchcock On BEST ANSWER

There's a hidden function inside the default Cake parent model that checks for fields with the name 'created' and 'modified', and updates the values automagically.

Search inside /lib/Cake/Model/Model.php for the string 'modified', and you'll see lots of references to this.

I'm not sure how well this is documented, I came across it while debugging some funky saving behavior.

In my system I settled on the fields 'created_at' and 'modified_at' before I found this little gem.

EDIT: This is documented here: http://book.cakephp.org/2.0/en/models/saving-your-data.html#using-created-and-modified

0
Anubhav On

Check if AppModel.php contains function save(){} and UpdateAll(){}. Next check datatype of created and modified fields in mysql. then modify your question for more clarity.