CakePHP数据验证创建和更新

时间:2013-07-25 08:33:36

标签: php cakephp cakephp-2.0

我正在使用cakePHP编写客户管理系统(我曾尝试使用此框架进行构建的第一件事),而且我在努力弄清楚如何在添加新客户时验证某些字段。 每个客户都有一个我需要手动添加的ID,以及一个必须唯一但可以为空的用户名。 这就是我想要发生的事情:

  1. 添加新客户时:
    • 检查该ID是否已存在并提醒用户是否存在(而不是添加用户)
    • 检查用户名是否已存在,如果确实存在,则提醒用户(而不是添加用户)
  2. 更新客户资料时(此时无法修改ID):
    • 如果用户名已被修改,请检查其是否已存在并提醒用户。
  3. 现在看来,每当我尝试添加具有现有ID的用户时,cakePHP只会用新信息覆盖现有id的信息。

    我尝试了几种验证选项,但似乎没有任何效果。

    这是第一个:

    public $validate = array(
            'id' => array(
                'idRule-1' => array(
                    'on' => 'create',
                    'rule' => 'uniqueID',
                    'message' => 'This Oracle ID already exists.',
                    'last' => false
                ),
                'idRule-2' => array(
                    'rule' => 'numeric',
                    'message' => 'Oracle ID can only contain numbers.',
                    'last' => false
                ),
            ), 
            'username' => array(
                'userRule-1' => array(
                    'on' => 'create',
                    'rule' => 'uniqueUser',
                    'message' => 'This username already exists',
                    'last' => false,
                ),
                'userRule-2' => array(
                    'on' => 'update',
                    'rule' => 'oneUser',
                    'message' => 'The eBay username you are trying to modify already belongs to another seller',
                    'last' => false,
                ),
            )
        );
    
    public function uniqueID() {
            return ($this->find('count', array('conditions' =>array('Seller.id' => $this->data['Seller']['id']))) == 0);
        }
    
        public function uniqueUser() {
            return ($this->find('count', array('conditions' =>array('Seller.username' => $this->data['Seller']['username']))) == 0);
        }
    
        public function oneUser() {
            return ($this->find('count', array('conditions' =>array('Seller.username' => $this->data['Seller']['username']))) == 1);
        }
    

    和第二个(仅用于id):

    public $validate = array(
            'id' => array(
                'unique' => array(
                    'rule' => 'isUnique',
                    'on' => 'create',
                    'message' => 'This Oracle ID already exists.',
                )
            )
        );
    

    以下是控制器的add()和edit()方法:

    public function add() {
        if ($this->request->is('post')) {
            $this->Seller->create();
            if ($this->Seller->save($this->request->data)) {
                $this->Session->setFlash(__('The seller has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The seller could not be saved. Please, try again.'));
            }
        }
        $accountManagers = $this->Seller->AccountManager->find('list');
        $primaries = $this->Seller->Primary->find('list');
        $thirdParties = $this->Seller->ThirdParty->find('list');
        $sites = $this->Seller->Site->find('list');
        $meetings = $this->Seller->Meeting->find('list');
        $this->set(compact('accountManagers', 'primaries', 'thirdParties', 'sites', 'meetings'));
    }
    
    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
        public function edit($id = null) {
            if (!$this->Seller->exists($id)) {
                throw new NotFoundException(__('Invalid seller'));
            }
            if ($this->request->is('post') || $this->request->is('put')) {
                $this->Seller->id = $id;
                if ($this->Seller->save($this->request->data)) {
                    $this->Session->setFlash(__('The seller has been saved'));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The seller could not be saved. Please, try again.'));
                }
                debug($this->Seller->validationErrors);
            } else {
                $options = array('conditions' => array('Seller.' . $this->Seller->primaryKey => $id));
                $this->request->data = $this->Seller->find('first', $options);
            }
            $accountManagers = $this->Seller->AccountManager->find('list');
            $primaries = $this->Seller->Primary->find('list');
            $thirdParties = $this->Seller->ThirdParty->find('list');
            $sites = $this->Seller->Site->find('list');
            $meetings = $this->Seller->Meeting->find('list');
            $this->set(compact('accountManagers', 'primaries', 'thirdParties', 'sites', 'meetings'));
        }
    

    非常感谢任何和所有提示!

1 个答案:

答案 0 :(得分:0)

如果您尝试保存的数据包含id,CakePHP会假定您正在更新现有记录。要在“create”上触发验证规则,不应设置id。如果设置了id,Cake将检查“更新”规则。

因此,您应该让数据库使用自动增量对新记录的ID进行影响,然后仅在编辑现有用户时管理验证。