Generell

Auch wenn die Migration auf TYPO3 LTS 9 ziemlich gut von der Community und dem Core team dokumentiert ist, hier eine Sammlung von Code Schnipseln, die ich immer wieder gebraucht habe. 

Vorab einige Links: 

/* TYPO3 DB DATA BASE */
see also Examples:
jans-blog.helke.de/2016/04/14/migrate-from-the-typo3-database-wrapper-to-the-doctrine-dbal-syntax/

Offical Documentation
docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Database/QueryBuilder/Index.html
docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Database/Migration/Index.html

 

typo3 LTS9 Snippets

hole alle Datensätze einer DB ohne mm verbindungen

$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('l18n_parent,header,bodytext', 'tt_content', "uid = '" . $actUid . "' " ); $response = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);

wird zu:
 

/** @var \TYPO3\CMS\Core\Database\ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance( "TYPO3\\CMS\\Core\\Database\\ConnectionPool");
/** @var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
$queryBuilder = $connectionPool->getConnectionForTable('tt_content')->createQueryBuilder();
$queryBuilder->select('l18n_parent','header','bodytext') ->from('tt_content') ;
$expr = $queryBuilder->expr();
$queryBuilder->where( $expr->eq('uid', $queryBuilder->createNamedParameter($actUid, \TYPO3\CMS\Core\Database\Connection::PARAM_INT)) ) ;

// Du brauchst die "Hidden" Datensätze auch ? oder auch welche ohne berücksichtung von Fe User Access Rechten ? // $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); $response = $queryBuilder->execute()->fetch();

Datensätze einfügen

$insertData = array ( "crdate" => time() , "feuser_uid" => intval($feUserUid) , "lesson_uid" => intval($lesson->getLocalizedUid()) ) ; $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_extension_domain_model_yourmodel' , $updateData);

wird zu:

/** @var \TYPO3\CMS\Core\Database\ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance( "TYPO3\\CMS\\Core\\Database\\ConnectionPool");

/** @var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
$queryBuilder = $connectionPool->getQueryBuilderForTable('tx_extension_domain_model_yourmodel') ;

/** @var \TYPO3\CMS\Core\Database\Connection $connection */
$connection = $connectionPool->getConnectionForTable('tx_extension_domain_model_yourmodel') ;
if ( $queryBuilder->insert('tx_extension_domain_model_yourmodel')->values($insertData)->execute() ) {
   return $connection->lastInsertId('tx_extension_domain_model_yourmodel') ;
} else {
   return 0 ;
}

 

Datensätze Updaten

/** @var \TYPO3\CMS\Core\Database\ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance( "TYPO3\\CMS\\Core\\Database\\ConnectionPool");
/** @var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
$queryBuilder = $connectionPool->getQueryBuilderForTable('fe_users') ;

/** @var \TYPO3\CMS\Core\Database\Connection $connection */
$connection = $connectionPool->getConnectionForTable('fe_users') ;

$queryBuilder ->update('fe_users')
->where( $queryBuilder->expr()->eq('tx_nem_cid', $queryBuilder->createNamedParameter($contactNo , Connection::PARAM_STR )) )
->set('usergroup', $usersgroups['usergroup'] ) ;

$queryBuilder->execute() ;

if ( !$connection->errorInfo() ) {
return TRUE;
} else {
  // $errorarr = array('faultstring' => 'Line: ' . __LINE__ . ' Error on update ', 'mode' => 'update', " error " => $connection->errorInfo() );
  return $errorarr;
}

Datensätze holen

/** @var \TYPO3\CMS\Core\Database\ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance( "TYPO3\\CMS\\Core\\Database\\ConnectionPool");

$connection = $connectionPool->getConnectionForTable('tx_jvchat_room') ;

/** @var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
$queryBuilder = $connectionPool->getQueryBuilderForTable('tx_jvchat_room') ;
$rows = $queryBuilder ->select('uid' , 'name') ->from('tx_jvchat_room')
->where( $queryBuilder->expr()->eq('name', $queryBuilder->createNamedParameter($roomName , Connection::PARAM_STR)) )
->orderBy("uid" , "DESC")
->setMaxResults(1)
->execute()
->fetchAll();

Datensätze zählen

$where = " sys_language_uid_list LIKE '" . $languageCode ."' ";
/** @var \TYPO3\CMS\Core\ $db */
$db = $GLOBALS['TYPO3_DB'] ;
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('COUNT(*)', 'tx_nemsolution_domain_model_tag', $where );
$row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);

 

// change to :
/** @var \TYPO3\CMS\Core\Database\ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance( "TYPO3\\CMS\\Core\\Database\\ConnectionPool");

/** @var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
$queryBuilder = $connectionPool->getQueryBuilderForTable('tx_nemsolution_domain_model_tag');
$userQuery = $queryBuilder->count( '*' )->from('tx_nemsolution_domain_model_tag' )
->where( $queryBuilder->expr()->like('sys_language_uid_list', $queryBuilder->createNamedParameter( $languageCode , \TYPO3\CMS\Core\Database\Connection::PARAM_STR )) ) ;

// .... if needed add more where conditions ...
if ( 1==1 ) {
   $userQuery->andWhere($queryBuilder->expr()->gt('crdate', 12345678 ) ) ;

}
$userQuery->execute()->fetchColumn(0);

Datensatz wirklich löschen

$res = $GLOBALS['TYPO3_DB']->exec_DELETEquery('fe_users', 'uid=' . $regform['uid']);

/// replace with
// use TYPO3\CMS\Core\Utility\GeneralUtility;
// use TYPO3\CMS\Core\Database\ConnectionPool;
// use TYPO3\CMS\Core\Database\Connection;

GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('fe_users')
->delete( 'fe_users', ['uid' => intval($regform['uid'])], [Connection::PARAM_INT] );

left Joins

$what = "fe_users.email, fe_users.campus_degree_program AS degreeprogram, fe_users.city AS city , fe_users.tx_nem_image AS nemImage, fe_users.tx_nem_gender as gender , fe_users.uid, fe_users.username , s.title as schoolname, s.country AS schoolcountry ," . " c." . $this->settings['country_field'] . " country_name , c.cn_iso_2 country_short "; $limit = 100 ;

$page = $page * $limit ;

$limit = $page . ",1000";

$from = "tx_nemcampus_domain_model_school s LEFT JOIN static_countries c ON s.country = c.uid LEFT JOIN tx_nemcampus_schools_feusers_mm mm ON mm.uid_local = s.uid LEFT JOIN fe_users ON mm.uid_foreign = fe_users.uid " ; $ignore = array ( "pid" => '1') ;

$where = "1=1 " . $GLOBALS['TSFE']->sys_page->enableFields('fe_users' , -1 , $ignore ) ; $order = "country_name, schoolname ASC" ;

$query = "SELECT " . $what . " FROM " . $from . " WHERE " . $where . " ORDER BY c." . $this->settings['country_field'] . " ASC" ; $res = $GLOBALS['TYPO3_DB']->sql_query($query);

 

// ########## replaced with ##################
/** @var \TYPO3\CMS\Core\Database\ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance( "TYPO3\\CMS\\Core\\Database\\ConnectionPool");

/** @var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
$queryBuilder = $connectionPool->getQueryBuilderForTable('tx_nemcampus_domain_model_school') ;

/** @var ExpressionBuilder $expr */
$expr = $queryBuilder->expr() ;

$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$queryBuilder->from('tx_nemcampus_domain_model_school' , 's')
->leftJoin( 's' , 'static_countries' , "c" , $expr->eq('s' . '.country', 'c.uid') )
->leftJoin( 's' , 'tx_nemcampus_schools_feusers_mm' , 'mm' , $expr->eq( 'mm.uid_local', 's.uid') )
->leftJoin( 'mm' , 'fe_users' , 'fe_users' , $expr->eq( 'mm.uid_foreign', 'fe_users.uid') )

->select("fe_users.email","fe_users.campus_degree_program AS degreeprogram" , "fe_users.city AS city" , "fe_users.tx_nem_image AS nemImage") ->addSelect("fe_users.tx_nem_gender as gender" ,"fe_users.uid" , "fe_users.username" , "s.title as schoolname" , "s.country AS schoolcountry")
->addSelect(" c." . $this->settings['country_field'] . " AS country_name" ,"c.cn_iso_2 AS country_short" )
->orderBy("c." . $this->settings['country_field'] )
// ->where($expr->eq( 's.hidden', '0'))
->andWhere($expr->eq( 'fe_users.disable', '0')) ;

$userRes = $queryBuilder->execute() ;

$userRows = $userRes->fetch() ;
// or $userRes->fetchAll() ;

 

Hardcoded SQL query notwendig? (not recommended!)

/** @var \TYPO3\CMS\Core\Database\ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance( "TYPO3\\CMS\\Core\\Database\\ConnectionPool");
/** @var \TYPO3\CMS\Core\Database\Connection $connection */
$connection = $connectionPool->getConnectionForTable('tx_nemsolution_domain_model_tag');

/** @var \TYPO3\CMS\Core\Database\Connection $connection */
$connection = $connectionPool->getConnectionForTable('tx_nemsolution_domain_model_tag');

$connection->exec("INSERT INTO `tx_nemsolution_domain_model_tag` ( `pid`, `tstamp`, `crdate`, `cruser_id`, `counter`, `deleted`, `hidden`, `word`, `metaphone`, `sys_language_uid_list`, `usergroup`) SELECT pid, tstamp, crdate, cruser_id, counter, deleted, hidden, word, metaphone, sys_language_uid_list, usergroup FROM tx_nemsolution_domain_model_tag_new;") ;


// oder Hoch komplexes Statement: $statement direct ausführen .. NOT Recommended
$row = $connection->executeQuery($statement)->fetch() ;

$lng = $GLOBALS['TSFE']->sys_language_uid

 

if (class_exists(\TYPO3\CMS\Core\Context\Context::class)) {

/** @var AspectInterface $languageAspect */  $languageAspect = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Context\Context::class)->getAspect('language') ;

    // (previously known as TSFE->sys_language_uid)
    $lng = $languageAspect->getId() ;
} else {
   $lng = $GLOBALS['TSFE']->sys_language_uid ;
}

ConfigurationManager get ContentObject()

if ( method_exists($this->configurationManager , "getContentObjectRenderer")) {
   $contentObject = $this->configurationManager->getContentObjectRenderer() ;

} else {
   $contentObject = $this->configurationManager->getContentObject() ;
}

backend: ersatz für $modTsConfig['properties'];

$modTsConfig = BackendUtility::getModTSconfig($id, 'mod.allplan_vcc') $this->configurationArray[$id] = $modTsConfig['properties'];

// change to
$this->configurationArray[$id] = BackendUtility::getPagesTSconfig($id)['mod.']['allplan_vcc.'] ?? [];

Backend: Module Url Ermitteln

/** * Returns the URL to a given module *
* @param string $moduleName Name of the module
* @param array $urlParameters URL parameters that should be added as key value pairs
* @return string Calculated URL */
public static function getModuleUrl($moduleName, $urlParameters = []) {
   /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
   $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);

   try {
       $uri = $uriBuilder->buildUriFromRoute($moduleName, $urlParameters);
   } catch (\TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException $e) {
      // no route registered, use the fallback logic to check for a module
      $uri = $uriBuilder->buildUriFromModule($moduleName, $urlParameters);
  }
   return (string)$uri;

}

PATH_site ist nicht mehr als Constante definiert

// replace PATH_site with :

(class_exists('TYPO3\\CMS\\Core\\Core\\Environment') ? (\TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/') : PATH_site) ;

Icons für Extensions werden nicht mehr dargestellt?

// OLD in TCA des Models
// 'icon'=>\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('nemjv_getcontent').'pi1/ce_wiz.gif',
 

// replace with :
// 'iconIdentifier' => 'ext-nemjvgetcontent-wizard-icon',

// und in der Ext_localconf.php
/** @var \TYPO3\CMS\Core\Imaging\IconRegistry $iconRegistry */
$iconRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Imaging\IconRegistry');
$iconRegistry->registerIcon( 'ext-nemjvgetcontent-wizard-icon', 'TYPO3\CMS\Core\Imaging\IconProvider\BitmapIconProvider', ['source' => 'EXT:nemjv_getcontent/Resources/Public/Icons/ce_wiz.gif'] );

alte Viewhelper funktionieren nicht mehr ?

Grund: // The Viewhelper extends should be changed from
// extends \TYPO3\CMS\Fluid\Core\ViewHelper
// to :
// extends \TYPO3Fluid\Fluid\Core\ViewHelper

 

Und die
render(string $releaseFor ) Funktion should have no arguments..
/** * Constructor *
* @api */
public function initializeArguments() {
   $this->registerArgument('releaseFor', 'string', 'A Usergroup name ', false); parent::initializeArguments() ;
}

 

// Zugriff auf das Argument dann mit:
$releaseFor = $this->arguments['releaseFor'] ;

Controller argument soll nicht validiert werden ?

// replace /* * @dontvalidate $post */ // with use TYPO3\CMS\Extbase\Annotation\IgnoreValidation; /* * @IgnoreValidation("post") */

Zugriff auf das ExtConf Array

if (class_exists(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)) {
   $this->extConf = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class) ->get('ke_search');
} else {
   $this->extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['ke_search']);

}