Dienstag, 17. November 2009 17:42
Wenn man über ein Backend-Modul Datensätze anlegt oder ändert, sollte man nicht einfach selber eine INSERT-Query schreiben, sondern die Funktionalität von TYPO3 nutzen. Bei Frontend-Plugins steht einem hierfür die globale Variable TYPO3_DB zur Verfügung. Für das Backend gibt es so etwas aber nicht, hier muss man etwas kreativer sein. Nach langer Einarbeitung in den TYPO3 Code und vielen Versuchen bin ich auf die recht simple Lösung gekommen:
/**
* Functions to use TYPO3 functionality for BE modules
*
* @author Sven Burkert (sventb@googlemail.com)
*/
class t3lib_db {
/**
* Insert new record
*
* @param string $table: table of new record
* @param array $fieldValues: values to insert, key is column of value
*/
public function t3lib_db_insert($table, array $fieldValues) {
$datamap = array($table => array(uniqid('NEW') => $fieldValues));
$this->t3lib_process($datamap);
}
/**
* Update existing record
*
* @param string $table: table of update record
* @param array $fieldValues: values to update, key is column of value
* @param integer $uid: uid of update record
*/
public function t3lib_db_update($table, array $fieldValues, $uid) {
$datamap = array($table => array($uid => $fieldValues));
$this->t3lib_process($datamap);
}
/**
* Delete a record
*
* @param string $table: table of record
* @param integer $uid: uid of delete record
*/
public function t3lib_db_delete($table, $uid) {
$this->t3lib_process(array(), array($table => array($uid => array('delete' => true))));
}
/**
* Undelete a record
*
* @param string $table: table of record
* @param integer $uid: uid of record to restore
*/
public function t3lib_db_undelete($table, $uid) {
$this->t3lib_process(array(), array($table => array($uid => array('undelete' => true))));
}
/**
* Move a record (to another page)
*
* @param string $table: table of record
* @param integer $uid: uid of record to move
* @param integer $pid: new page id for record
*/
public function t3lib_db_move($table, $uid, $pid) {
$this->t3lib_process(array(), array($table => array($uid => array('move' => $pid))));
}
/**
* Copy a record (to another page)
*
* @param string $table: table of record
* @param integer $uid: uid of record to copy
* @param integer $pid: new page id for record
*/
public function t3lib_db_copy($table, $uid, $pid) {
$this->t3lib_process(array(), array($table => array($uid => array('copy' => $pid))));
}
/**
* Localize a record
*
* @param string $table: table of record
* @param integer $uid: uid of record (default language) to localize
* @param integer $languageId: id of new language
*/
public function t3lib_db_localize($table, $uid, $languageId) {
$this->t3lib_process(array(), array($table => array($uid => array('localize' => $languageId))));
}
/**
* Process input
*
* @see t3lib_tcemain->process_datamap()
* @param array $datamap: Data to be modified or inserted in the database
*/
private function t3lib_process(array $datamap, array $cmd = array()) {
$t3lib_tcemain = t3lib_div::makeInstance('t3lib_tcemain');
$t3lib_tcemain->start($datamap, $cmd);
$t3lib_tcemain->process_datamap();
$t3lib_tcemain->process_cmdmap();
}
}
Diese Klasse kann man nun zum Anlegen neuer Datensätze,…
require_once(t3lib_extMgm::extPath('myExt') . 'class.t3lib_db.php');
$t3lib_db = new t3lib_db();
$t3lib_db->t3lib_db_insert(
'myTableName',
array(
'pid' => $myPid,
'title' => 'bla',
'myField' => 'blubb'
)
);
… zum Aktualisieren bestehender Datensätze und…
require_once(t3lib_extMgm::extPath('myExt') . 'class.t3lib_db.php');
$t3lib_db = new t3lib_db();
$t3lib_db->t3lib_db_insert(
'myTableName',
array(
'pid' => $myPid,
'title' => 'bla',
'myField' => 'blubb'
),
$myUid
);
…zum Löschen von Datensätzen und…
require_once(t3lib_extMgm::extPath('myExt') . 'class.t3lib_db.php');
$t3lib_db = new t3lib_db();
$t3lib_db->t3lib_db_delete(
'myTableName',
$myUid
);
…zum Verschieben von Datensätzen usw. nutzen:
require_once(t3lib_extMgm::extPath('myExt') . 'class.t3lib_db.php');
$t3lib_db = new t3lib_db();
$t3lib_db->t3lib_db_move(
'myTableName',
$myUid,
$newPid
);
Damit werden die Datensätze korrekt in die Tabelle eingefügt bzw. aktualisiert, d.h. mit den Timestamps, korrekten Wert für Feld “sorting” und korrektem Handling des Workspaces bzw. Versionierung (betrifft die Felder “t3ver_*”). Vor allem die Handhabung der Datensätze im Entwurfs-Workspace und anderen Workspaces wird damit erleichtert.
Beachten sollte man, dass man nun jedes Feld im TCA definieren muss, denn sonst werden die Werte für dieses Feld verworfen. Felder, die man nicht über das Backend abändern darf, kann man als “nur lesen” anlegen, z.B. so:
'config' => Array (
'type' => 'input',
'readOnly' => 1
)
oder so:
'config' => Array (
'type' => 'select',
'foreign_table' => 'myTable',
'size' => 1,
'readOnly' => 1
)
EDIT 09.03.2010: Habe eben den Artikel auf http://blog.tolleiv.de/2010/03/handling-data-in-typo3-with-tcemain/ entdeckt und die weiteren Funktionen (Datensatz verschieben, kopieren und übersetzen) nachgereicht.