[TYPO3-mvc] Repository with external database

Henk Scholten henk at wolfsstyle.nl
Tue Oct 1 10:49:41 CEST 2013


Hello,

I'm pretty new to extbase ans was wondering if this is the right way to 
to call an external database in my repository and put the results in the 
model with extbase:

<?php
namespace Ws\FfPersons\Domain\Repository;

class PersonRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {

	public function initializeObject() {
		$this->conf = 
unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['ff_persons']);
		$this->db = 
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');

		try {
			$this->db->setDatabaseHost($this->conf['hostname']);
			$this->db->setDatabaseUsername($this->conf['username']);
			$this->db->setDatabasePassword($this->conf['password']);
			$this->db->sql_pconnect();
		} catch (\TYPO3\CMS\Fluid\Core\ViewHelper\Exception $exception) {
			throw new 
\TYPO3\CMS\Fluid\Core\ViewHelper\Exception($exception->getMessage());
		}

		try {
			$this->db->sql_select_db($this->conf['database']);
		} catch (\TYPO3\CMS\Fluid\Core\ViewHelper\Exception $exception) {
			throw new 
\TYPO3\CMS\Fluid\Core\ViewHelper\Exception($exception->getMessage());
		}
	}

	/**
	 * findAll
	 *
	 * @return
	 */
	public function findAll() {
		$select = array();
		$select['fields'] = '*';
		$select['table'] = 'persons';
		$select['where'] = '';
		$select['groupby'] = '';
		$select['orderby'] = 'surname';
		$res = $this->db->exec_SELECTquery($select['fields'], 
$select['table'], $select['where'], $select['groupby'], $select['orderby']);

		$persons = array();
		while ($rw = $this->db->sql_fetch_assoc($res)) {
			$personObj = new \Ws\FfPersons\Domain\Model\Person;
			$personObj->setFullname($rw['naam_volledig']);
			$personObj->setNickname($rw['roepnaam']);
			$persons[] = $rw;
		}

		return $persons;
	}

}
?>

I read something about an persistence layer but don't know how to use that.

All suggestions are welcome!

Regards,
Henk


More information about the TYPO3-project-typo3v4mvc mailing list