This language has a decent CMS with very advance features in it ,which works great for the end user and it has many components ready to use online world and it is possible that you want have multiple components in it ........
The first step
I'll start with an clean Joomla 1.6 install from the Joomla website. Also I installed Doctrine ORM from the Doctrine website. I unpacked the archive in a new folder in the Joomla folder libraries.The structure in this new folder would be the same as the archive, so you get a Doctrine folder and a bin folder in the libraries/doctrine folder. By the way, we live in 2011 and the current release of PHP is 5.3.5. Doctrine and the implementation I show you here assumes you use PHP 5.3. If you still use PHP 5.2 you seriously need to work on the upgrade of that projects.The example
I'm going to provide you a very simple example. The most important thing I want to show you is how to implement Doctrine in your Joomla project, not how to use Doctrine. The documentation on the Doctrine website is of high quality and should give you enough information to get going. We are writing a new component called Bugs end hold one entity called 'Bug'. It has five attributes. ID, title, description, notificationDate and solvedDate. In the controller I'll show you some examples how to use it.Setting up the defaults
It is possible that you want to use Doctrine on multiple components you are going to write. This is the reason why I choose to add Doctrine to the libraries folder. In this folder we are going to add two files. One is an interface to provide a generic interface, the other is a bootstrapper to get Doctrine going. First the easy stuff, the interface. The interface looks like this:
interface JoomlaDoctrineController {
public function setEntityManager(Doctrine\ORM\EntityManager $entityManager);
/**
* Configuration class to integrate Doctrine into Joomla.
*
* @author pderaaij
*/
use Doctrine\Common\ClassLoader,
Doctrine\ORM\EntityManager,
Doctrine\ORM\Configuration,
Doctrine\Common\Cache\ArrayCache;
if( !class_exists('\Doctrine\Common\Classloader')) {
require_once dirname(__FILE__) . '/../doctrine/Doctrine/Common/ClassLoader.php';
}
class JoomlaDoctrineBootstrapper {
const APP_MODE_DEVELOPMENT = 1;
const APP_MODE_PRODUCTION = 2;
private $applicationMode;
private $cache;
private $entityLibrary;
private $proxyLibrary;
private $proxyNamespace;
private $entityManager;
private $connectionOptions;
public function __construct($applicationMode) {
$this->applicationMode = $applicationMode;
}
public function getConnectionOptions() {
return $this->connectionOptions;
}
public function setConnectionOptions($connectionOptions) {
$this->connectionOptions = $connectionOptions;
}
public function getProxyLibrary() {
return $this->proxyLibrary;
}
public function setProxyLibrary($proxyLibrary) {
$this->proxyLibrary = $proxyLibrary;
}
public function getProxyNamespace() {
return $this->proxyNamespace;
}
public function setProxyNamespace($proxyNamespace) {
$this->proxyNamespace = $proxyNamespace;
}
public function getCache() {
return $this->cache;
}
public function setCache($cache) {
$this->cache = $cache;
}
public function getEntityLibrary() {
return $this->entityLibrary;
}
public function setEntityLibrary($entityLibrary) {
$this->entityLibrary = $entityLibrary;
}
public function getApplicationMode() {
return $this->applicationMode;
}
public function setApplicationMode($applicationMode) {
$this->applicationMode = $applicationMode;
}
public function getEntityManager() {
return $this->entityManager;
}
public function setEntityManager($entityManager) {
$this->entityManager = $entityManager;
}
/**
* Bootstrap Doctrine, setting the libraries and namespaces and creating
* the entitymanager
*/
public function bootstrap() {
$this->registerClassLoader();
// Load cache
if ($this->getApplicationMode() == self::APP_MODE_DEVELOPMENT) {
$this->cache = new ArrayCache;
} else {
$this->cache = new ApcCache;
}
/** @var $config Doctrine\ORM\Configuration */
$config = new Configuration;
$config->setMetadataCacheImpl($this->cache);
$driverImpl = $config->newDefaultAnnotationDriver($this->getEntityLibrary());
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($this->cache);
$config->setProxyDir($this->getProxyLibrary());
$config->setProxyNamespace($this->getProxyNamespace());
if ($this->applicationMode == self::APP_MODE_DEVELOPMENT) {
$config->setAutoGenerateProxyClasses(true);
} else {
$config->setAutoGenerateProxyClasses(false);
}
$this->entityManager = EntityManager::create($this->getConnectionOptions(), $config);
}
/**
* Register the different classloaders for each type.
*/
private function registerClassLoader() {
// Autoloader for all the Doctrine library files
$classLoader = new ClassLoader('Doctrine', dirname(__FILE__) . '/');
$classLoader->register();
// Autoloader for all Entities
$modelLoader = new ClassLoader('Entities', $this->getEntityLibrary());
$modelLoader->register();
// Autoloader for all Proxies
$proxiesClassLoader = new ClassLoader('Proxies', $this->getProxyLibrary());
$proxiesClassLoader->register();
}
}
Creating the component
Now we can start with setting up our component. Create a com_bugs folder in components with a models folder in it. In the models folder you should create an Entities folder (mind the capital, it's important!). Create the 'Bug' entity next. Copy the text below and place it in Bug.php in the models/Entities folder. Do not forget to set the namespace it is essential. If you forget it the entity can't be found by the classloader. This is also the reason why the capitalization is so important.
namespace Entities;
/**
* @Entity @Table(name="bugs")
*/
class Bug {
/** @Id @Column(type="integer") @GeneratedValue */
private $id;
/** @Column(type="string") */
private $title;
/** @Column(type="string") */
private $description;
/** @Column(type="datetime") */
private $notificationDate;
/** @Column(type="datetime") */
private $solvedDate;
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function getTitle() {
return $this->title;
}
public function setTitle($title) {
$this->title = $title;
}
public function getDescription() {
return $this->description;
}
public function setDescription($description) {
$this->description = $description;
}
public function getNotificationDate() {
return $this->notificationDate;
}
public function setNotificationDate($notificationDate) {
$this->notificationDate = $notificationDate;
}
public function getSolvedDate() {
return $this->solvedDate;
}
public function setSolvedDate($solvedDate) {
$this->solvedDate = $solvedDate;
}
}
The configuration
In order to use Doctrine within our controllers we have to create the configuration and use our created bootstrapper to get Doctrine going. At the moment I chose the component initialization file for our initialization work on Doctrine. I'm looking for a way to extract this initialization in to a nicer place, but the options I've tried didn't made me happy at all. What do we need to do? Well, we need to tell where our entities are stored on the file system and where we want to store our proxy files. also we need to pass our database parameters to Doctrine so it has authorization to the database. Here is my init file (bugs.php) of the component:
// no direct access
defined('_JEXEC') or die;
// Include dependancies
jimport('joomla.application.component.controller');
require_once(JPATH_LIBRARIES . '/doctrine/bootstrap.php');
$controller = JController::getInstance('Bugs');
// configure Doctrine thru the bootstrapper
$controller->setEntityManager(bootstrapDoctrine());
$controller->execute(JRequest::getCmd('task', 'index'));
$controller->redirect();
/**
* Initialize doctrine by setting the entities and proxies locaties. Also define
* a default namespace for the proxies.
*/
function bootstrapDoctrine() {
$doctrineProxy = new JoomlaDoctrineBootstrapper( JoomlaDoctrineBootstrapper::APP_MODE_DEVELOPMENT );
$doctrineProxy->setEntityLibrary(dirname(__FILE__) . '/models');
$doctrineProxy->setProxyLibrary(dirname(__FILE__) . '/proxies');
$doctrineProxy->setProxyNamespace('Joomla\Proxies');
$doctrineProxy->setConnectionOptions(getConfigurationOptions());
$doctrineProxy->bootstrap();
return $doctrineProxy->getEntityManager();
}
function getConfigurationOptions() {
// Define database configuration options
$joomlaConfig = JFactory::getConfig();
return array(
'driver' => 'pdo_mysql',
'path' => 'database.mysql',
'dbname' => $joomlaConfig->get('db'),
'user' => $joomlaConfig->get('user'),
'password' => $joomlaConfig->get('password')
);
}

