2017-05-23 54 views
1

我有TYPO3 7.6.18版本。TYPO3如何設置fe_user的用戶組?

我正在定製登記和我在控制器中添加用戶在我的代碼:

$this->userRepository->add($user); 

我需要設置用戶組

我想:

$user->setUsergroup(1); 

但它調用的錯誤:

PHP Catchable Fatal Error: Argument 1 passed to TYPO3\CMS\Extbase\Domain\Model\FrontendUser::setUsergroup() must be an instance of TYPO3\CMS\Extbase\Persistence\ObjectStorage, integer given, called in /home/abenteuer/public_html/typo3conf/ext/feusersplus/Classes/Controller/NewController.php on line 68 and defined in /home/abenteuer/public_html/typo3/sysext/extbase/Classes/Domain/Model/FrontendUser.php line 192 

我知道我應該將參數傳遞給setUsergroup(),它必須是TYPO3 \ CMS \ Extbase \ Persistence \ ObjectStorage的實例,但我不知道該怎麼做。

請幫助我,任何人。我想設置用戶組,例如,與UID 1

回答

2

你所需要的用戶羣體單獨的存儲庫(或與現有FrontendUserGroupRepository開始)來檢索組(S):

$userGroup = $this->userGroupRepository->findByIdentifier(1); 

然後,你可以簡單地做到以下幾點:

$user->addUsergroup($userGroup); 

然後添加$user到你的資料庫像以前一樣。

+0

非常感謝您!但現在我看到另一個錯誤! ()必須是TYPO3 \ CMS \ Extbase \ Persistence \ ObjectStorage的實例,In2code \ Femanager \ Domain \ Model \ UserGroup,在第70行調用/home/abenteuer/public_html/typo3conf/ext/feusersplus/Classes/Controller/NewController.php並在/ home/abenteuer/public_html/typo3/sysext/extbase/Classes/Domain/Model/FrontendUser.php line 192 – Mikael

+0

我也試過addUsergroup和我得到錯誤: PHP致命錯誤:調用成員函數attach()在非對象在/ home/abenteuer/public_html/typo3/sysext/extbase /類/域/模型/ FrontendUser.php – Mikael

+0

@Mikael如果你想使用'setUsergroup()',你需要自己準備一個'ObjectStorage'實例。由於'usergroup'屬性的'ObjectStorage'是在'FrontendUser'構造函數中創建的,因此'addUsergroup'錯誤不應該發生。 –

0

我目前無法評論,但是你忘了一些東西,所以我創建了一個答案。

你只能使用$this->userGroupRepository->findByIdentifier(1);如果你注入庫到控制器用下面的代碼:

/** 
* userGroupRepository 
* 
* @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserGroupRepository 
* @inject 
*/ 
protected $userGroupRepository = null;