Showing posts with label fe_users. Show all posts
Showing posts with label fe_users. Show all posts

Friday, December 21, 2012

TYPO3 Extbase - mapping existing fields from fe_users

Extbase comes with an own domain model and repository to access the frontend users (fe_users) and frontend groups (fe_groups) tables. If you install sr_feuser_register, several extra fields are added to the fe_users table. Those extra fields are not included in extbase default domain model for fe_users, so you have to extend the domain model with the fields you want to access. This article show howto extend the existing domain model and some things to keep in mind.

Preperation

The first thing to do is to create a new domain model, which extends the existing one. Create a new class for this in your extensions Domain\Model folder.

class Tx_Testext1_Domain_Model_FrontendUser extends Tx_Extbase_Domain_Model_FrontendUser {

}

Next you need to create a repository for the new domain model. Create this in the Domain\Repository folder of your extension.

class Tx_Testext1_Domain_Repository_FrontendUserRepository extends Tx_Extbase_Domain_Repository_FrontendUserRepository {

}

The next thing to do is to setup the mapping for the new table, so Extbase knows from which table it has to get the fields. Add the following to your sites typoscript setup.

config.tx_extbase.persistence.classes {
    Tx_Testext1_Domain_Model_FrontendUser {
        mapping {
            tableName = fe_users
        }
    }
}

You also need to configure, in which storage pid the frontend users are stored. You can set this in the typoscript constants of your extension.

plugin.tx_testext1 { 
  persistence.storagePid = 100
}

Extending the existing domain model
Now you're ready to extend the domain model Tx_Extbase_Domain_Model_FrontendUser to access the fields, which are not included in the default domain model. Actually you can also extend the domain model with new fields, but this is not part of this article.

Lets assume, you want to access the field "comments", which was created by sr_feuser_register. The only thing to do is to add a getter and a setter for this field in the newly created domain model.

class Tx_Testext1_Domain_Model_FrontendUser extends Tx_Extbase_Domain_Model_FrontendUser {

 /**
  * Comment
  *
  * @var string
  */
 protected $comment;

 /**
  * Setter for comment
  *
  * @param string $comment
  * @return void
  */
 public function setComment ($comment) {
  $this->comment = $comment;
 }

 /**
  * Getter for comment
  *
  * @return string
  */
 public function getComment () {
  return $this->comment;
 }
}

Now you can inject the newly created repository to get a record from the database.

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

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

/**
 * Test action
 *
 * @return void
 */
public function testAction () {
 $user = $this->userRepository->findByUid(1);
 t3lib_utility_Debug::debug($user);
}

When you call the action showed above, you should see the debug output of the user object from the user with the uid 1. In the debug output, you should see the additional field "comment", which is included in the newly created domain model.

Fields with underscore and mixed case
The extension sr_feuser_register adds some additional field to fe_users, which include underscores. The same does "felogin", which actually adds fields with underscores and mixed case (e.g. "felogin_forgotHash").

If you want to include fields with underscore in your domain model (e.g. "date_of_birth"), make sure to remove the underscore and to set a Uppercase of the next letter instead.

/**
 * Date of birth
 *
 * @var int
 */
protected $dateOfBirth;

/**
 * Setter for date_of_birth
 *
 * @param int $dateOfBirth
 * @return void
 */
public function setDateOfBirth ($dateOfBirth) {
 $this->dateOfBirth = $dateOfBirth;
}

/**
 * Getter for date_of_birth
 *
 * @return int
 */
public function getDateOfBirth () {
 return $this->dateOfBirth;
}

If you have fields with underscore and mixed case, just remove the underscore. Extbase seems to ignore the case.

Here is an example of the field "felogin_forgotHash"

/**
 * Password forgot hash
 *
 * @var string
 */
protected $feloginForgothash;



Sunday, June 3, 2012

Zugriff auf Frontend User Tabelle aus Extbase

Extbase bindet die TYPO3 fe_users Tabelle schon automatisch mit ein und stellt entsprechende Objekte bereit. Man muss jedoch ein paar Dinge beachten, damit man auch auf diese Daten zugreifen kann.

Als erstes muss man in seiner Klasse das Users-Repository per Dependency Injection zu Verfügung stellen.

/**
 * @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;
}  

Somit hat man dann Zugriff auf das Repository. Kennt man nun z.B. die UID des FE Users, kann man wie folgt die Userdaten über das Repository auslesen.

$user = $this->userRepository->findByUid($uid);

Wer sich nun wundert, warum die Variable $user leer ist, obwohl sie einen User enthalten müsste, der muss folgendes beachten.

Extbase stellt die Daten zwar per default zur Verfügung, geht aber davon aus, dass alle FE User auch einen Record Type zugewiesen haben.

config.tx_extbase.persistence.classes {
 Tx_Extbase_Domain_Model_FrontendUser {
  mapping.recordType = Tx_Extbase_Domain_Model_FrontendUser
 }
}

Beim installieren von Extbase wird sowohl die Tabelle fe_users als auch fe_groups um genau dieses Feld (tx_extbase_type) erweitert. Existierende als auch neue User haben natürlich keinen recordType zugewiesen (siehe Bild), sodass das UserRepository demzufolge auch nichts zurückliefern kann.







Nun gibt es zwei Möglichkeiten mit diesem "Problem" umzugehen. Entweder man setzt in allen FE Usern den Record Type, oder man hebt das von Extbase vorgegebene mapping wie folgt auf.

config.tx_extbase.persistence.classes {
 Tx_Extbase_Domain_Model_FrontendUser {
  mapping.recordType >
 }
}

Ich würde aber ganz klar dazu raten, die User entsprechend richtig anzulegen (sprich das Feld Record Type gleich bei Anlage des/der FE User zu setzen). Damit man das nicht manuell machen muss, kann man die Felder schon vorbelegen. Dazu einfach folgendes TS ins PageTS der Root-Seite einfügen.

TCAdefaults.fe_users.tx_extbase_type = Tx_Extbase_Domain_Model_FrontendUser
TCAdefaults.fe_groups.tx_extbase_type = Tx_Extbase_Domain_Model_FrontendUserGroup

So kann man übrigens auch beliebige Felder bei Neuanlage von Datensätzen vorbelegen.