在 Zend_Auth 中,我可以获得域模型 User 对象而不是 stdClass 吗?

发布于 2024-08-01 13:31:57 字数 700 浏览 2 评论 0原文

在处理应用程序的登录部分时,我发现自己遇到了一个问题:

我的模型基于 Active Record,通过扩展 Zend_Db_Table_Row 对象。 每当我必须与用户打交道时,我都想通过用户对象(作为扩展表行)来完成此操作。 但是,Zend_Auth_Adapter_DbTable::getResultRowObject() 返回一个 stdClass,我找不到一种方法来告诉 Zend_Auth_Adapter_DbTable 使用特定的 Db_Table。

到目前为止我发现的三个选项:

  1. 编写一个自定义的 Zend_Auth_Adapter,以返回正确的类对象(在内部使用 Zend_Db_Table)。

  2. 身份验证后,执行 stdClass 的一些读取和 User 对象的一些写入 - 您可以创建一个 Zend_Db_Table_Row 类,而不实际使用 Zend_Db_Table 来创建它吗?

    身份
  3. 从 Active Record 转向完整的域驱动开发,将我的数据访问与用户对象分开。 然后编写第二个映射器,用 stdClass 而不是表行填充用户对象。

我的两个问题:您会推荐哪一个? 我缺少什么吗?

我不想做任何这些,然后发现我在框架中遗漏了一些明显的东西。 有谁知道是否有“正确”的方法来做到这一点?

问候, 桑德尔

While working on the login part of an application, I find myself running into an issue:

My model is based on Active Record, by extending Zend_Db_Table_Row objects. Whenever I have to deal with a user, I'd want to do this through the User object (being an extended table row). However, Zend_Auth_Adapter_DbTable::getResultRowObject() returns a stdClass and I can not find a way to tell Zend_Auth_Adapter_DbTable to use a specific Db_Table.

My three options that I've found so far:

  1. Write a custom Zend_Auth_Adapter, to return the proper class object (using Zend_Db_Table internally).

  2. After authentication perform some reading of the stdClass and some writing of the User object - can you create a Zend_Db_Table_Row class without actually using the Zend_Db_Table to create it?

  3. Move away from Active Record into complete Domain Driven development, separating my data access away from the user object. Then write a second mapper to fill the User object with stdClass instead of the table row.

My two questions: Which of these would you recommend? And is there something I am missing?

I'd hate to do any of these and then find out I'm missing something obvious in the Framework. Does anyone know if there is a 'proper' way to be doing this?

Regards,
Sander

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

書生途 2024-08-08 13:31:57

特别是,我使用解决方案号 2,我有一个包装 Zend_Auth 的自定义 Auth 对象,因此在登录时,我有以下内容:

public function login($user, $password) {
    if(!($this->adapter instanceof Zend_Auth_Adapter_DbTable)) {
        throw new Gecko_Auth_Exception("The Adapter is not valid or not initialized");
    }

    $auth = Zend_Auth::getInstance();

    $this->adapter->setIdentity($user);
    $this->adapter->setCredential($password);

    $result = $auth->authenticate($this->adapter);
    if( $result->isValid() ) {
        $data = $this->adapter->getResultRowObject(null, $this->passwordColumn);
        $userObject = new self::$userClass($data);
        $auth->getStorage()->write($userObject);
        return true;
    } else {
        return false;
    }
}

然后我可以调用 Zend_Auth::getInstance 来检索自定义 UserClass 对象,以便我可以将其映射到我的自定义Zend_Db_Table_Row 类。

希望能帮助到你

In particullar, I use solution number 2, I have a custom Auth object that wraps Zend_Auth, so on login, I have this:

public function login($user, $password) {
    if(!($this->adapter instanceof Zend_Auth_Adapter_DbTable)) {
        throw new Gecko_Auth_Exception("The Adapter is not valid or not initialized");
    }

    $auth = Zend_Auth::getInstance();

    $this->adapter->setIdentity($user);
    $this->adapter->setCredential($password);

    $result = $auth->authenticate($this->adapter);
    if( $result->isValid() ) {
        $data = $this->adapter->getResultRowObject(null, $this->passwordColumn);
        $userObject = new self::$userClass($data);
        $auth->getStorage()->write($userObject);
        return true;
    } else {
        return false;
    }
}

Then I can call Zend_Auth::getInstance to retreive the custom UserClass object so I can have it mapped yo my custom Zend_Db_Table_Row class.

Hope it helps

归途 2024-08-08 13:31:57

没有“正确”的方法来做你想做的事。 您展示的方法应该都有效。

就我个人而言,我赞成在 Zend_Auth 中只存储用户的 ID - 如果您要存储包含所有用户详细信息的完整对象,那么如果用户要更改其信息,它就会不同步。

在一个项目中,我曾经编写了一个控制器插件,它为用户提供了控制器。 它基本上从 Zend_Auth 获取 ID 并基于它构建一个用户对象。 它还创建了一个匿名用户对象,以防没有 ID。 并不是说这是最好的方法,但它是透明且有效的。

There is no "proper" way to do what you want. The approaches you show should all work.

Personally I'm in favor of storing just an ID for the user in Zend_Auth - if you were to store a full object with all user details, it would fall out of sync if the user was to change their info.

In a project I once wrote a controller plugin which provided the user to the controllers. It basically took the ID from Zend_Auth and built a user object based on it. It also created an anonymous user object in case there was no ID. Not saying this is the best approach, but it's transparent and worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文