Model view controller 在Zend 1.11中,我是在Model还是DB Model Mapper中进行验证检查? 在下面的情况下,我有一个模型和模型映射器,对于在哪里放置验证函数,我有点困惑,例如,考虑下面的: models -> User -> UserMapper Class User{ private $id; private $name; private $email; } Class UserMapper extends Zend_Db_Table_Abstract{ function findById($id){ } function save(User $user){ } }

Model view controller 在Zend 1.11中,我是在Model还是DB Model Mapper中进行验证检查? 在下面的情况下,我有一个模型和模型映射器,对于在哪里放置验证函数,我有点困惑,例如,考虑下面的: models -> User -> UserMapper Class User{ private $id; private $name; private $email; } Class UserMapper extends Zend_Db_Table_Abstract{ function findById($id){ } function save(User $user){ } },model-view-controller,zend-framework,Model View Controller,Zend Framework,我需要对其进行设置,以使新用户对象不能与现有用户对象具有相同的电子邮件-我应该将验证逻辑放在哪里来检查这一点,即在模型或模型映射器中?会不会是: Class UserController{ function doAction(){ $user = new User(); $u = new UserMapper(); ... // is this the right way to do this? if($user->validate()){

我需要对其进行设置,以使新用户对象不能与现有用户对象具有相同的电子邮件-我应该将验证逻辑放在哪里来检查这一点,即在模型或模型映射器中?会不会是:

Class UserController{

  function doAction(){
    $user = new User();
    $u = new UserMapper();
    ...
    // is this the right way to do this?
    if($user->validate()){

    }

    // or is this the right way to do this?

   if($u->validate($user)){

   }

}
}

任何与数据库表相关的复杂逻辑或验证都应转到“模型”类(用户)。不要将它们放在表类(UserMapper)中

在您的例子中,模型类名是“User”

我遵循以下方法进行重复电子邮件检查

  • 在模型文件(User.php)中包含“Model Mapper”文件(UserMapper.php)
  • 在模型类(用户)内创建名为“isEmailDuplicate($emailAddress)”的公共静态方法。在这个函数中,创建映射器类的对象(UserMapper.php),并在映射器类对象(UserMapper.php)上使用“Zend_Db_select”()执行选择查询。最后将布尔结果返回给调用者
  • 从UserController调用模型函数。 $isEmailDuplicate=User::isEmailDuplicate($emailAddress)
    通常,您会在注册表中看到这种验证,您可以使用注册表对象进行验证,也可以使用用户对象

    无论哪种方式,都可能被证明是有用的,并且可以用作表单验证程序:

    //form demo
    class Reg_Form extends Zend_Form
    {  
    public function init() {
       $name = new Zend_Form_Element_Text('name');
            $name->setLabel('Name');
            $name->setAttrib('placeholder', 'Username');
            $name->setOptions(array('size' => 20));
            $name->addFilter('StringToLower');
            //truncated for brevity
            $name->addValidator(new Zend_Validate_Db_NoRecordExists(array(
                        'table' => 'users',
                        'field' => 'name'
                    )));
            $this->addElement($name);
        }
    }
    
    或者模型中的独立验证器

    //Entity Model demo, This is used to check against MP3 database
    /**
     * Does record already exist in DB
     *
     * Pass in the mapper to use as a string.
     * Will trigger predefined validator DbNoRecordExists
     *
     * @param string $mapper accepted values are: 'album' 'artist' 'track'
     * @return boolean returns true if no record exists
     */
    protected function dbNoExist($mapper)
    {
        switch ($mapper) {
            case 'album':
                $value = $this->taginfo->getAlbum();
                $options = array(
                    'table'  => 'album',
                    'field'  => 'title'
                );
                break;
            case 'artist':
                $value = $this->taginfo->getArtist();
                $options = array(
                    'table'  => 'artist',
                    'field'  => 'name'
                );
                break;
            case 'track':
                $value = $this->taginfo->getMd5();
                $options = array(
                    'table' => 'track',
                    'field' => 'hash'
                );
        }
        $validator = new Zend_Validate_Db_NoRecordExists($options);
        if ($validator->isValid($value)) {
            //no record exists
            return TRUE;
        } else {
            //record exists
            return FALSE;
        }
    }
    
    我在项目中尝试回答这些问题的方式是:

    如果我将数据持久性从MySql更改为平面文件(或其他方法),是否仍需要执行此操作(验证)

    如果是这样,代码将进入实体模型(用户)。如果没有,则代码将进入映射器。我意识到这有点过于简单,但它通常会让我朝着正确的方向前进

    [编辑]

    就我个人而言,如果可能的话,我会在表单发布之前做一点验证。我想在用户发布表单之前让用户知道他是否已经拥有该电子邮件的帐户,这样可以节省我们的时间和沮丧。最终验证始终可以在用户模型中完成


    祝你好运。

    我喜欢在模型中使用静态方法的想法,但是模型不代表用户对象吗。从逻辑上讲,这是用户对象将要做的事情吗?数据库中的唯一键约束如何?