Posted (Updated ) in PHP

While aimlessly browsing my Twitter feed last night, I came across this tutorial on using CodeIgniter‘s Zip class. Kohana 3.1 doesn’t seem to come with similar functionality out of the box (at least from the brief glance I took), so I figured I’d write up a quick patch to get CI’s class working with it.

Download the module with example controller and test files here.

You can read up on how to use the class in CodeIgniter’s documentation or the tutorial linked to above. I’ve included a controller and 2 test files in the download above as a kind of ‘quick start guide’. To access them simply go to /koziptest and /koziptest/invalid for a successful and unsuccessful download respectively.

There isn’t really any more to say about this one other than the fact that I made the methods in my module chainable for convenience and consistency with other Kohana classes. Note that this means the read_file, read_dir and archive methods will now throw exceptions if you use an invalid file/directory path or your input or output files aren’t readable/writable – so remember to use a try..catch block! I’ve also made the download method take a Request variable as its first argument – simply pass your controllers current request to it like so:

$Zip->download( $this->response )

Read More »

Posted (Updated ) in Database, PHP

NOTE: If you’re looking for the Doctrine 2 modules for K3 see this post.

Download the module here.

I’ve previously written a module for Kohana 3 for Doctrine, as well as drivers for Kohana’s Auth and Session modules, however I’ve finally had the time to merge the three and fix up some longstanding issues present with them; namely the folder structure and lack of PDO support. To make my life a little easier I’ve moved the project to Google Code too.

You can find the project here. It’s got Doctrine 1.23 already in there, you should just be able to drop it in and add it to your bootstrap file. It also includes sample schema and data fixtures for the kohana auth and session modules – they can be found in /models/fixtures.

Good luck!

Read More »

Posted (Updated ) in Database, PHP

Update Feb 22 2012: 1.0.8 is out. Now works with and requires Doctrine 2.2. Thanks to Markus for his patch.

Since releasing my original Doctrine 2 module for Kohana 3, I’ve done a bit of reshuffling of folders and added some additional features from my Doctrine 1.2 module. Due to the extent of modifications, I decided to put up a new post with some added information on how to use the new module.

Features:

  • Doctrine 2 integration (obviously)
  • Auth and Session drivers
  • /doctrine controller to load your schema files and generate the DB tables
  • Works in Kohana 3.0 and 3.1

For the impatient ones out there, here’s the download link:
Doctrine 2 Module for Kohana 3

 

As always, install the module (remembering to add it to your bootstrap file) and download the latest copy of Doctrine 2 from here, placing into the /modules/doctrine2/classes/vendor/doctrine folder. It should be noted that my module is tailored to the tarball download and not the SVN or Github ones which have a slightly different folder structure.

Read More »

Posted (Updated ) in Database, PHP

UPDATE 28 Dec 2010: This module is now defunct. Please instead use the Better Doctrine 2 Module for Kohana 3.

For those of you who don’t already know, Doctrine 2 stable was just released. It features what is essentially a complete rewrite and paradigm shift from the 1.2 version and comes with a laundry list of improvements. You can read the official blog post here. To celebrate the occasion I decided to do up a quick Kohana 3 module. Info & specz below:

Firstly the configuration. You’ll need the following files & folders:
/doctrine/Entities (These are essentially (to my meager knowledge) your model files.)
/doctrine/Proxies
/modules/doctrine/classes/doctrine
/modules/doctrine/init.php

Download the latest copy of Doctrine 2 from here and extract into the /modules/doctrine/classes/doctrine folder.

Here’s /modules/doctrine/init.php

<?php
	use Doctrine\ORM\EntityManager,
		Doctrine\ORM\Configuration;
 
	class Doctrine
	{
		private static $_instance = null;
		private $_application_mode = 'development';
		private $_em = array();
 
		/*
		 * Rename $conn_name to whatever you want your default DB connection to be
		 */
		public static function em( $conn_name = 'default' )
		{
			if ( self::$_instance === null )
				self::$_instance = new Doctrine();
 
			return isset(self::$_instance->em[ $conn_name ])
				? self::$_instance->em[ $conn_name ]
				: reset(self::$_instance->_em);
		}
 
		public function __construct()
		{
			require __DIR__.'/classes/doctrine/Doctrine/Common/ClassLoader.php';
 
			$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', __DIR__.'/classes/doctrine');
			$classLoader->register();
			//This allows Doctrine-CLI tool & YAML mapping driver
			$classLoader = new \Doctrine\Common\ClassLoader('Symfony', __DIR__.'/classes/doctrine/Doctrine');
			$classLoader->register();
			//Load entities
			$classLoader = new \Doctrine\Common\ClassLoader('Entities', APPPATH.'doctrine');
			$classLoader->register();
 
			//Set up caching method
			$cache = $this->_application_mode == 'development'
				? new \Doctrine\Common\Cache\ArrayCache
				: new \Doctrine\Common\Cache\ApcCache;
 
			$config = new Configuration;
			$config->setMetadataCacheImpl( $cache );
			$driver = $config->newDefaultAnnotationDriver( APPPATH.'doctrine/Entities' );
			$config->setMetadataDriverImpl( $driver );
			$config->setQueryCacheImpl( $cache );
 
			$config->setProxyDir( APPPATH.'doctrine/Proxies' );
			$config->setProxyNamespace('Proxies');
			$config->setAutoGenerateProxyClasses( $this->_application_mode == 'development' );
 
			$dbconfs = Kohana::config('database');
 
			foreach ( $dbconfs as $conn_name => $dbconf )
				$this->_em[ $conn_name ] = EntityManager::create(array(
					'dbname' 	=> $dbconf['connection']['database'],
					'user' 		=> $dbconf['connection']['username'],
					'password' 	=> $dbconf['connection']['password'],
					'host' 		=> $dbconf['connection']['hostname'],
					'driver' 	=> 'pdo_mysql',
				), $config);
		}
	}

Once the module is enabled, you can access Doctrine from anywhere with the following code:

Doctrine::em()

The module supports multiple databases and uses the Kohana database config file for connection details. To grab any specific database use the following (where connection_name is the name specified in the config file)

Doctrine::em('connection_name')

Download the complete module here.

Hope this helps.

Read More »

Posted (Updated ) in PHP

As with any project involving generation of .xlsx spreadsheets there’s only really one way to go – PHPExcel. This amazing piece of software boasts a large feature set – allowing quick and (relative) painless generation of XLS, XLSX, PDF, and CSV to name a few as well as importing of the same sans PDF. I recently had to integrate PHPExcel into Kohana 3 so I figured I’d take the time to post the how-to and hopefully make your lives that little bit easier. In addition I’ve included a quick and dirty Spreadsheet class to make XLSX generation that bit faster.

Read More »