Pergunta

I want to store a username in a coupon object, each coupon object already has the uid of the user who created it. I can loop over the coupon objects and read the associated usernames from fe_users but how then will I save those usernames into the coupons so when they are passed to the template the usernames can be read like so coupon.username, or in some other easy way so each username will appear on the page with the right coupon as they are all printed out in a table?

If I was doing basic php instead of typo3 i would just define a query but what is the typo3 v4.5 way?

My code so far - which dies on the line where I try to assign the new property --creatorname -- to the $coup object.

public function listAction() {
        $coupons = $this->couponRepository->findAll();
                // @var Tx_Extbase_Domain_Repository_FrontendUserRepository $userRepository */
                $userRepository = $this->objectManager->get("Tx_Extbase_Domain_Repository_FrontendUserRepository");
                foreach ($coupons as $coup) {
                    echo '<br />test '.$coup->getCreator(); 
                    echo '<br />count = '.$userRepository->countAll().'<br />';
                    $newObject =  $userRepository->findByUid( intval($coup->getCreator()));
                    //var_dump($newObject);

                    var_dump($coup);
                    echo '<br />getUsername '.$newObject->getUsername() ; 
                    $coup['creatorname'] = $newObject->getUsername();
                     echo '<br />creatorname '.$coup['creatorname'] ; 
                }
        $this->view->assign('coupons', $coupons);
    }
Foi útil?

Solução

Make the Frontenduser a Property of your Model

This is my prefered solution to this. You have already a Ceator in your Coupon Model which seems to be only an integer value. You can make this a real relation to a fe_user.

Model:

/**
 * @var Tx_Extbase_Domain_Model_FrontendUser
 */
protected $creator;

/**
 * @param Tx_Extbase_Domain_Model_FrontendUser $creator
 * @return void
 */
public function setCreator(Tx_Extbase_Domain_Model_FrontendUser $creator) {
    $this->creator = $creator;
}

/**
 * @return Tx_Extbase_Domain_Model_FrontendUser
 */
public function getCreator() {
    return $this->creator;
}

TCA: In the TCA-Configuration of your coupon table you change the creator column to

...    
'creator' => array(
      'label' => 'your/label/from/locallang.xml',
      'config' => array(
        'type' => 'select',
        'foreign_table' => 'fe_users'
      )
    ),
...

After this you can set a hole FrontendUser object to that coupon of yours. This will also work with your existing coupons because in the db still only the uid of the fe_user is stored. Only Typo3 (via TCA) and extbase (via model) are told that this is a relation to fe_users. So $coupon->getCreator() returns a FrontendUser now. But you dont have to call this in your controller, just pass your $coupons to the view. You can access the username of a creator in Fluid with

{coupon.creator.username}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top