0

我有一个与表 UserDetail 相关的用户表。 User(#id, name, password) UserDetail(#id, address, city, user_id)

UserDetail.user_id是链接到的外键User.id

如果我想添加一个新地址,这是我填写地址的表格:

<?php echo $this->Form->create('UserDetail');?>
<?php echo $this->Form->hidden('id'); ?>
<?php echo $this->Form->input('address', array('class' => 'form-control')); ?>
<?php echo $this->Form->input('city', array('class' => 'form-control')); ?>
<?php echo $this->Form->hidden('user_id'); ?>
<?php echo $this->Form->button('Modifier', array('class' => 'btn btn-primary')); ?>
<?php echo $this->Form->end(); ?>

我的控制器:

public function customer_edit($id = null){
    if (!$this->User->exists($id)) {
        throw new NotFoundException('Invalid user details');
    }
    if ($this->request->is('post') || $this->request->is('put')) {
        $this->request->data['UserDetail']['user_id'] = $id;
        if ($this->User->UserDetail->save($this->request->data)) {
            $this->Flash->success('Done!');
            return $this->redirect(array('action' => 'dashboard'));
        } else {
            $this->Session->setFlash('Error.');
        }
    } 
}

而且我总是有闪存“错误”。if ($this->User->UserDetail->save($this->request->data))不起作用。如果我看一下$request->dataDebugKit,我有所有的输入数据。

添加新记录不起作用...问题出在哪里?

谢谢。

编辑:这是我的 UserDetail 类:

class UserDetail extends AppModel {
    public $validate = array(
        'id' => array(
            'numeric' => array(
                'rule' => array('numeric')
            ),
        ),
        'address' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'allowEmpty' => false,
            ),
        ),
        'city' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'allowEmpty' => false,
            ),
        ),
        'user_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
    );
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'counterCache' => true,
            'counterScope' => array(),
        )
    );
}

和 UserDetail 发布数据内容:

>用户详情

ID

地址测试

城市测试测试

用户 ID 19

4

1 回答 1

1

通常您不想为主键 Model::id 设置验证规则。但是,如果您这样做,则必须仅在更新记录时强制执行,而不是在创建记录时强制执行(在这种情况下为空)。

尝试如下重写您的验证数组:

public $validate = array(
    'id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            'on' => 'update' //don't enforce rule on create
        ),
    ),
    'address' => array(
        'notempty' => array(
            'rule' => array('notEmpty'),
            'allowEmpty' => false,
        ),
    ),
    'city' => array(
        'notempty' => array(
            'rule' => array('notEmpty'),
            'allowEmpty' => false,
        ),
    ),
    'user_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
        ),
    ),
);

顺便说一句,在同一规则中使用 and 是多余的'rule' => array('notEmpty')'allowEmpty' => false您可以删除后者。

话虽如此,我建议您重新考虑您的观点和行动,因为它们似乎没有按照我的预期执行。

于 2015-12-03T09:37:06.687 回答