这是我的第一个 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'
),
)
)
谢谢