1

これは私の最初のCakePhpプロジェクトであり、日付に問題があります。

私のデータベースは生年月日(dob)の1つのフィールドであるため、ユーザーがログインすると、日付配列は次のように格納されますが[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

「日付、月、年で区切らずに、日付を1つの文字列として配列に格納したいのですが」

問題はない。CakePHPごとに意図的にそのように保存しますが、単一の日付フィールドに正しく保存されます。

于 2013-03-26T13:59:44.023 に答える
1

OK、あなたのコメントによると、日付を単一の文字列に戻す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 に答える