【问题标题】:PHP Symfony - Form processing questionsPHP Symfony - 表单处理问题
【发布时间】:2010-10-01 08:01:37
【问题描述】:

我正在尝试构建自己的用户身份验证系统(仅仅是因为那里的系统过于复杂和庞大)。

不过,我很难掌握 Symfony 表单处理。 我正在查看 sfDoctrineGuardPlugin,但对于我的生活,无法弄清楚,输入的密码在保存到数据库之前何时转换为 SHA1 哈希。

我在哪里可以阅读有关表单处理和学说可能在两者之间执行的自动魔法的内容?我一直在看“A Gentle Introduction to Symfony”,但它并没有真正的帮助。

我发现,它发生在 updateObject() 方法的某个地方。

    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter($this->form->getName()));
      if ($this->form->isValid())
      {
        var_dump($this->form->getObject()->password);
        $this->form->updateObject();
        var_dump($this->form->getObject()->password);
      }
    }
// Prints:
// null
// string '989d88b585ce29839687f2938303e828e191ecef' (length=40)

但我无法找到该方法的实现,以及它究竟调用/做什么。

谁能解释一下?我只是想了解 Symfony 在后台做了什么。我认为发生了太多的魔法,有时缺少文档。

【问题讨论】:

    标签: php symfony1


    【解决方案1】:

    http://trac.symfony-project.org/browser/plugins/sfDoctrineGuardPlugin/branches/1.3/lib/model/doctrine/PluginsfGuardUser.class.php#L33

      public function setPassword($password)
      {
        if (!$password && 0 == strlen($password))
        {
          return;
        }
    
        if (!$salt = $this->getSalt())
        {
          $salt = md5(rand(100000, 999999).$this->getUsername());
          $this->setSalt($salt);
        }
        $modified = $this->getModified();
        if ((!$algorithm = $this->getAlgorithm()) || (isset($modified['algorithm']) && $modified['algorithm'] == $this->getTable()->getDefaultValueOf('algorithm')))
        {
          $algorithm = sfConfig::get('app_sf_guard_plugin_algorithm_callable', 'sha1');
        }
        $algorithmAsStr = is_array($algorithm) ? $algorithm[0].'::'.$algorithm[1] : $algorithm;
        if (!is_callable($algorithm))
        {
          throw new sfException(sprintf('The algorithm callable "%s" is not callable.', $algorithmAsStr));
        }
        $this->setAlgorithm($algorithmAsStr);
    
        parent::_set('password', call_user_func_array($algorithm, array($salt.$password)));
      }
    

    【讨论】:

    • 伙计,我怎么会在那里找到它? ;) 所以当你调用 save() 时,Doctrine 实际上为每个字段调用 setValue?在代码中哪里可以看到? (只是想落后于自动魔法)。感谢您的帮助。
    • 从来没有深入研究过学说的代码来回答这个问题,但我已经深入研究了这个插件的代码,知道你原来问题的答案。
    • 谢谢。我想只会这样使用它而不是问问题;)
    • @tillman:我无法在这里给出答案,但您想了解 Doctrine 的访问器/突变器并查看 Doctrine_Record、Doctrine_Record_Abstract 和 Doctrine_Access 上的 __set/_set/set 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2021-12-05
    相关资源
    最近更新 更多