1

这是我的第一个 CakePhp 项目,我对日期有疑问..

我的数据库作为出生日期(dob)的一个字段,因此当用户登录时,日期数组的存储就像[dob] => 1992-04-24但是当他们更改日期时,它会变成[dob] => Array ( [month] => 04 [day] => 24 [year] => 1992 )

我认为这与我的编辑字段或模型有关,但我无法弄清楚。所以这里是模型:

<?php

App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');

/**
 * User Model
 *
 * @property Group $Group
 */
class User extends AppModel {

    public $validationDomain = 'validation';

    public $belongsTo = array('Group');
    public $actsAs = array('Acl' => array('type' => 'requester'));

    public function parentNode() {
        if (!$this->id && empty($this->data)) {
            return null;
        }
        if (isset($this->data['User']['group_id'])) {
            $groupId = $this->data['User']['group_id'];
        } else {
            $groupId = $this->field('group_id');
        }
        if (!$groupId) {
            return null;
        } else {
            return array('Group' => array('id' => $groupId));
        }
    }

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'username' => array(
            'notempty' => array(
                'rule' => array('notempty'),
            ),
        ),
        'password' => array(
            'notempty' => array(
                'rule' => array('notempty'),
            ),
        ),
        'name' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Your name is required'
            )
        ),
        'dob' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'please enter your date of birth'
             )
        ),
            'group_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
        )
    ),
    );

    public function bindNode($user) {
        return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);
    }

    public function beforeSave($options = array()) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        return true;
    }

}

这是编辑字段:

<div class="edit user form">
<?php
echo $this->Form->create('User', array('action' => 'useredit'));
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->input('username', array('label' => __('Username', true), 'value' => $this->Session->read('Auth.User.username')));
echo $this->Form->input('password', array('label' => __('Password', true), 'value' => ''));
echo $this->Form->input('name', array('label' => __('Name', true), 'value' => $this->Session->read('Auth.User.name')));
echo $this->Form->input('dob', array ('label' => __('Date Of Birth', true) 
                                        , 'value' => $this->Session->read('Auth.User.dob')
                                        , 'dateFormat' => 'YMD'
                                        , 'minYear' => date('Y') - 90
                                        , 'maxYear' => date('Y') - 0));
echo $this->Form->end(__('Submit'));
?>
</div>

以防万一您想要调试器报告,它是:

array(
    'User' => array(
        'password' => '*****',
        'id' => '2',
        'username' => 'iwanjones',
        'name' => 'Iwan',
        'dob' => array(
            'year' => '2013',
            'month' => '03',
            'day' => '26'
        ),
    )
)

我希望调试器将数组保存为

array(
    'User' => array(
        'password' => '*****',
        'id' => '2',
        'username' => 'iwanjones',
        'name' => 'Iwan',
        'dob' => '2013/03/26'
        ),
    )
)

谢谢

4

2 回答 2

2

“好吧,我希望将日期作为一个字符串存储在数组中,而不是按日期、月份和年份分隔”

没有问题。它按照 CakePHP 有意地存储它,但会正确地保存在单个日期字段中。

于 2013-03-26T13:59:44.023 回答
1

好的,根据您的评论,您需要在 User 模型中添加一个 beforeSave 方法,将日期重新转换为单个字符串:

function beforeSave($options = array()) {
    // Convert the dob back to a single string
    if (!empty($this->data[$this->alias]['dob'])) {
        $this->data[$this->alias]['dob'] = implode('-',
            $this->data[$this->alias]['dob']['year'],
            $this->data[$this->alias]['dob']['month'],
            $this->data[$this->alias]['dob']['day']
        );
    }

    return true;
}
于 2013-03-26T13:05:57.260 回答