简体   繁体   中英

Tx_Extbase_Domain_Repository_FrontendUserRepository->findAll() not working in typo3 4.5.30?

I am trying to run a simple query off of the Tx_Extbase_Domain_Repository_FrontendUserRepository. I cannot get anything to work except findByUid(), not even findAll().

In my controller I have this code which seems to work:

/**
* @var Tx_Extbase_Domain_Repository_FrontendUserRepository 
*/
protected $userRepository;

/**
* Inject the user repository
* @param Tx_Extbase_Domain_Repository_FrontendUserRepository $userRepository 
* @return void */
public function injectFrontendUserRepository(Tx_Extbase_Domain_Repository_FrontendUserRepository $userRepository) {
$this->userRepository = $userRepository;
}

/**
* action create
*
* @param Tx_BpsCoupons_Domain_Model_Coupon $newCoupon
* @return void
*/
public function createAction(Tx_BpsCoupons_Domain_Model_Coupon $newCoupon) {
...... some code .....
$user = $this->userRepository->findByUid(($GLOBALS['TSFE']->fe_user->user[uid]));
$newCoupon->setCreator($user);
...... some code .....
}

but in another function I want to look up a user not by uid but by a fe_users column called vipnumber (an int column) so I tried

/**
* check to see if there is already a user with this vip number in the database
* @param string $vip
* @return bool
*/
public function isVipValid($vip) {
echo "<br/>" . __FUNCTION__ . __LINE__ . "<br/>";
echo "<br/>".$vip."<br/>";

//$ret = $this->userRepository->findByUid(15); //this works!! but
$query = $this->userRepository->createQuery(); 
$query->matching($query->equals('vip',$vip) ); 
$ret = $query->execute(); //no luck
.................

and neither does this

$ret = $this->userRepository->findAll();

How can one work but not the others? In my setup I already put config.tx_extbase.persistence.classes.Tx_Extbase_Domain_Model_FrontendUser.mapping.recordType > which seems to be necessary for the fiondByUid to work, is it preventing the other from working?

I am using typo3 v 4.5.30 with extbase 1.3

Thanks

If $this->userRepository->findByUid(15); works, there is no reason why $this->userRepository->findAll(); should not. However $this->userRepository->findAll(); returns not a single Object but a collection of all objects, so you have to iterate over them.

If you add a column to the fe_users, you have to add it to TCA and to your extbase model (you need a getter and a setter), too! After that you can call findByProperty($property) in your repository. In your case that would be

$user = $this->userRepository->findByVipnumber($vip);

This will return all UserObjects that have $vip set as their Vipnumber. If you just want to check if that $vip is already in use, you can call

$user = $this->userRepository->countByVipnumber($vip);

instead. Which obviously returns the number of Users that have this $vip;

You never use $query = $this->createQuery(); outside your Repository.

To add the property to the fronenduser Model you create your own model Classes/Domain/Model/FronendUser.php:

class Tx_MyExt_Domain_Model_FrontendUser extends Tx_Extbase_Domain_Model_FrontendUser {
  /**
   * @var string/integer
   */
  protected $vipnumber;
}

Add a getter and a setter. Now you create your own FrontendUserRepository and extend the extbase one like you did with the model. You use this repository in your Controller. Now you're almost there: Tell Extbase via typoscript, that your model is using the fe_users table and everything should work:

config.tx_extbase {
    persistence{
        Tx_MyExt_Domain_Model_FrontendUser{
            mapping {
                tableName = fe_users
            }
        }
    }
}

To disable storagePids in your repository in general, you can use this code inside your repository:

/**
 * sets query settings repository-wide
 * 
 * @return void
 */
public function initializeObject() {
    $querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings');
    $querySettings->setRespectStoragePage(FALSE);
    $this->setDefaultQuerySettings($querySettings);
}

After this, your Querys will work for all PIDs.

I didn't have the opportunity to work with frontend users yet, so I don't know if the following applies in this case:

In a custom table I stumbled uppon the fact, that extbase repositories automatically have a look at the pids stored in each entry and check it against a set storage pid (possibly also the current pid if not set). Searching for a uid usually means you have a specific dataset in mind so automatic checks for other values could logically be ignored which would support your experiences. I'd try to set the storage pid for your extension to the place the frontend users are stored in ts-setup:

plugin.[replace_with_extkey].persistence.storagePid = [replace_with_pid]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM