Cake Bake,如何将日期创建添加到SQL?

时间:2014-06-12 15:42:12

标签: sql cakephp

我觉得人们误解了我的问题

很明显我看到了 $这 - >用户 - >保存($这 - >请求 - >数据 但模型中没有任何东西可以节省当前时间,cakephp如何知道呢?它正在读取sql说嗯这个说创建时,这个保存我会添加一个日期是发生了什么?

所有这些代码都是通过终端烘焙蛋糕生成的,我只是想学习和修改

蛋糕如何保存创建日期?那行代码在哪里?

我的sql设置为

id int(10)
email varchar(64) 用户名varchar(50) 密码varchar(50) 角色varchar(20)
创建日期时间
修改日期时间

这是我的模特

<?php
// app/Model/User.php
App::uses('AppModel', 'Model');

class User extends AppModel {

    public $useTable = 'User'; //this decide the table used

    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required'
            )
        ),
        'email' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A email is required'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('admin', 'author')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        )
    );
}

这是我的添加

控制器
  public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        }
    }

我的观点

<div class="users form">
<?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend><?php echo __('Add User'); ?></legend>
    <?php
        echo $this->Form->input('username');
        echo $this->Form->input('password');
        echo $this->Form->input('role', array(
            'options' => array('admin' => 'Admin', 'editor' => 'Editor', 'guest' => 'Guest')
        ));
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
    <h3><?php echo __('Actions'); ?></h3>
    <ul>

        <li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
    </ul>
</div>

2 个答案:

答案 0 :(得分:1)

当您使用以下

$this->User->save($this->request->data)

保存方法检查,如果未设置创建和修改,则在mysql查询执行之前将其设置为当前日期时间。

答案 1 :(得分:0)

由于时间戳行为被添加到Cake烘焙模型中(如果您在表中有创建和修改的列),它会更新这些字段。

class ArticlesTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');
    }
}

您可以在此处找到更多相关信息:http://book.cakephp.org/3.0/en/orm/behaviors/timestamp.html