0

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 i t preventing the other from working?

I am using typo3 v 4.5.30 with extbase 1.3

Thanks

The Newbie Qs
  • 483
  • 8
  • 22

2 Answers2

1

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.

Daniel
  • 6,916
  • 2
  • 36
  • 47
  • Hi Daniel, Thanks for responding. My extension does not add a column to fe_users but uses a column added by another extension. how would I add that to TCA or my model? Is there somewhere I need to add something somewhere about recordtype or storagepid? – The Newbie Qs Nov 22 '13 at 16:23
  • why not use $query = $this->userRepository->createQuery(); outside of its own repository? it is a public method. – The Newbie Qs Nov 22 '13 at 20:31
  • Well if that column was added by another extension, then most likely the TCA is taken care off (you can always check in your backend. Configuration->TCA->fe_users->columns. Your field must apear there. As for the model i edit my answer. – Daniel Nov 22 '13 at 22:14
  • the column was added by a p1 style extension with its own TCA stuff. Is there a compatibility issue since I am using extbase? maybe I need to remove that old extension and roll its features into mine or a new extbase extension. what do you think? and thanks for the help. – The Newbie Qs Nov 25 '13 at 15:25
  • and yes the column is present under Configuration->TCA->fe_users->columns, so that's good, but the countBy* and findBy* functions don't work either. Should they be called by the full database column name? or the label in the TCA? Should my setup set the Tx_Extbase_Domain_Model_FrontendUser recordype to null? or to what? I set it to null earlier to make something else work. – The Newbie Qs Nov 25 '13 at 16:00
  • after a little more experimenting I see that findByName works off my coupon repo but findByUsername does not work off my user repo... do I need to add my own frontend user model since another extension added the columns and TCA? trying that next.. – The Newbie Qs Nov 25 '13 at 16:12
  • after some more attempts I get this error: #1247602160: Table 'devc.tx_myext_domain_model_frontenduser' doesn't exist: SELECT tx_myext_domain_model_frontenduser.* FROM tx_myext_domain_model_frontenduser WHERE tx_myext_domain_model_frontenduser.username = 'asdasd' Tx_Extbase_Persistence_Storage_Exception_SqlError thrown in file /home/typo3_src/typo3_src-4.5.30/typo3/sysext/extbase/Classes/Persistence/Storage/Typo3DbBackend.php in line 1008. – The Newbie Qs Nov 25 '13 at 17:54
  • this meansyour frontenduser-Model is not mapped to the fe_users table in the database. So the table tx_myext_domain_model_frontenduser is expected. Use the Typoscript Snippet from my answer to deal with that. – Daniel Nov 25 '13 at 18:05
  • And findBy* and countBy* are used with the properties from your model. If they differ from the database columns names, you have to map the properties via typoscript, too. – Daniel Nov 25 '13 at 18:07
  • I have this in my setup, config.tx_extbase { persistence{ Tx_MyExt_Domain_Model_FrontendUser{ mapping { tableName = fe_users } } } } but get that message anyway. What about this line also from my setup: config.tx_extbase.persistence.classes.Tx_Extbase_Domain_Model_FrontendUser.mapping.recordType > – The Newbie Qs Nov 25 '13 at 18:23
  • You are aware, that Tx_MyExt_Domain_Model_FrontendUse is just an example? Is has to be the exact class name of your frontendUser Model. And disabling a default recordType is fine. You dont need record types. – Daniel Nov 25 '13 at 18:45
  • Yes, I use my actual model name. a similar example is here http://www.derhansen.de/2012/12/typo3-extbase-mapping-existing-fields.html but i still get the same results. – The Newbie Qs Nov 25 '13 at 18:47
  • changed typoscript to : config.tx_extbase.persistence.classes { Tx_BpsCoupons_Domain_Model_FrontendUser { mapping { tableName = fe_users } } } and now I get no error but no result from $ret = $this->userRepository->findByUsername('cory')->toArray(); either :( var_dump($ret) shows an empty array. showing actual ext name there. – The Newbie Qs Nov 25 '13 at 18:53
  • If I need to specify a storage pid for users, how can I do so after already specifying a pid for my coupons? eg in constants plugin.tx_bpscoupons { persistence.storagePid = 109 //for coupons } – The Newbie Qs Nov 25 '13 at 20:13
  • the sql for a call to $this->userRepository->findByUsername('myname') looks like this: SELECT fe_users.* FROM fe_users WHERE fe_users.username = ? AND fe_users.deleted=0 AND fe_users.disable=0 AND fe_users.starttime<=1385415540 AND (fe_users.endtime=0 OR fe_users.endtime>1385415540) AND fe_users.pid IN (109) so its losing the name passed in and uses pid f 109 instead of 19. why? – The Newbie Qs Nov 25 '13 at 21:45
  • ahh, the ? is supposed to still be there, so its not losing the name, the issue is just the use of pid==109 , how should this be specified? – The Newbie Qs Nov 25 '13 at 22:01
  • I updated my answer with how to disable the respectStoragePid Option – Daniel Nov 26 '13 at 15:47
  • does that go in a repo, or my controller? – The Newbie Qs Nov 26 '13 at 15:59
  • I put it in my frontenduserrepo, AND IT WOOOOORRRKSSS!!!! THANK YOU THANK YOU THANK YOU!!!!!!!!!!!! – The Newbie Qs Nov 26 '13 at 16:10
0

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]
gsnerf
  • 573
  • 1
  • 4
  • 15