2014-06-26 17 views
1

我在我的zf2應用程序中使用Doctrine和MongoDB ODM模塊。 ZfcUser用於授權。如何使用兩個集合來驗證zfcuser?

有沒有辦法使用兩個集合,說usersclients通過zfcuser +原則認證?我很好奇,如果有一種方法可以將兩個mongo集合合併爲一個用於合併進行身份驗證?

+0

我對你的工作沒有任何體驗,但是在純mongodb中你只會做兩個查詢。 – Jarema

回答

2

你不需要合併th Ë用戶到一個集合,你可以有多個「認證適配器」(見ZfcUser\Authentication\Adapter\Db爲例)

這些全局配置文件中定義的:zfcuser.global.php

我的每一個適配器都在優先級順序運行,直到一個返回成功的身份驗證結果。

例如;我有UsersCandidates實體的以下配置。

/**  
* Authentication Adapters 
* 
* Specify the adapters that will be used to try and authenticate the user 
* 
* Default value: array containing 'ZfcUser\Authentication\Adapter\Db' with priority 100 
* Accepted values: array containing services that implement 'ZfcUser\Authentication\Adapter\ChainableAdapter' 
*/ 
'auth_adapters' => array( 
    50 => 'JobboardCandidate\Authentication\Adapter\CandidateDatabaseAdapter', 
    75 => 'JobboardUser\Authentication\Adapter\UserDatabaseAdapter', 
    //100 => 'ZfcUser\Authentication\Adapter\Db', [this is the default] 
), 
1

正如我從你的問題所理解的,你想得到一個具有兩種不同文檔類型的集合,以便你可以使用這個集合進行驗證。 如果您使用原則繼承映射您可以擁有兩個不同的類並在一個集合中解析它們。 在這種情況下,您的Client課程將延長您的User課程。如果你想使用findAll方法在你UserRepository你會得到雙方的客戶和用戶一個Collection

這將幫助你實現你想要的:

<?php 
namespace MyProject\Model; 

/** 
* @Document 
* @InheritanceType("SINGLE_COLLECTION") 
* @DiscriminatorField(name="discriminator", type="string") 
* @DiscriminatorMap({"user" = "User", "client" = "Client"}) 
*/ 
class User 
{ 
    // ... 
} 

/** 
* @Document 
*/ 
class Client extends User 
{ 
    // ... 
} 

然後

$userRepository->findAll(); 

閱讀更多關於繼承映射here in the Doctrine documentation

+0

爲什麼你提到實體,ORM等?我使用MongoDB Doctrine ODM。但ODM也有繼承映射 - http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/inheritance-mapping.html –

+0

對不起我的錯誤,我以爲你在使用Doctrine ORM2。但我認爲你確實可以對Doctrine ODM繼承映射做同樣的事情。這個概念完全一樣。 – Wilt

+0

我更新了我的答案。 – Wilt

相關問題