Posted (Updated ) in PHP

If you’ve ever come across the infuriating error

htmlspecialchars(): Invalid multibyte sequence in argument

I have a simple solution for you: Turn display_errors on in your php.ini file!

It turns out there’s a weird bug that doesn’t appear to be getting fixed any time soon that causes htmlspecialchars() to display this error only when display_errors is set to Off.

See this post for further details and a very big thank you to the Andy Young for writing it and saving me (and I’m sure many others) alot of time!

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

I’ve been working with the Zend_Gdata tools lately to perform various Google Calendar operations. One thing I’ve noticed, though, is that there are no tutorials on how to cache the auth request between pages. Caching the authentication would result in one less expensive operation to an external server upon each new file request (The retrieving of a new, working Zend_Gdata_HTTPClient object). Figuring out how to accomplish this seemingly simple task proved a little more difficult than I’d have liked and ironically ended up being very simple 🙂

Read More »

Posted (Updated ) in Database, PHP

Here’s my second Doctrine 1.2 driver for Kohana 3, this time for the Session class. I wrote this because I don’t particularly like the Kohana Database or ORM classes (that or I just don’t want to take the time to learn them!) and it’s released under a creative commons v3 licence So here it is. Enjoy!

Kohana 3 Doctrine Session Driver 1.02 See here for details

UPDATE 22 Oct 2010: Bugfix release – should actually work now 🙂
UPDATE 03 Jan 2011: Fixed rare bug in DB cleaning function.
UPDATE 21 Feb 2011: New module at a new home! See here for details

Read More »

Posted (Updated ) in PHP

I’ve recently started looking at the Google Calendar Data API for PHP for use in an upcoming project. It uses Zend Framework which has a pretty nice library complete with documentation here allowing for just about every operation I required…except deleting calendars (which is a bit strange, considering you’re able to create them with the API). This seems to be a limitation with version 1.0 of the API. Version 2 allows for deletion of calendars however there’s no documentation PHP implementation yet. After a long night of researching and hair pulling I’ve finally managed to come up with a working script provided.

EDIT – Now working with Zend Framework!

Read More »

Posted (Updated ) in PHP

A very quick post for any of you poor souls suffering the dreaded ‘Discovery Failed’ message in Zend_OpenID_Consumer when trying to connect to Google – see this comment on Stack Overflow for the solution you’re looking for. Hopefully this’ll save you some time hunting around on the web for it.

Read More »

Posted (Updated ) in PHP

Have you ever wanted to generate a resized version of an image just using a HTML image tag and not worrying about entering large chunks of resizing code or storing cached images of various sizes? Today’s tutorial will show you exactly how to do that with Kohana 3. In this tutorial I will teach how to use the URL http://www.mysite.com/sized/image/// to load a dynamically generated, resized image.

The trickiest concept to grasp in this tutorial are the URL segments. Imagine your site was located at http://www.mysite.com and you were browsing to this page http://www.mysite.com/archive/posts/my-post. In this instance, ‘segment 1’ would be archive, segment 2 posts and segment 3 my-post. Segments are split with the / character. In Kohana, URL segments take the following form: controller/method/method_arg_1/method_arg_2/etc…

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 »

Posted (Updated ) in PHP

This is quite possibly one of the simplest integrations in Doctrine history (It’s a 1 liner, folks). The following is a short tutorial on how to create a SwiftMailer module for Kohana 3

Create the following files/folders:
/modules/swiftmailer
/modules/swiftmailer/init.php
/modules/swiftmailer/classes

Inside /modules/swiftmailer/classes/ drop the official latest build of Swift Mailer.

Enter the following into init.php.

<?php
	require Kohana::find_file('classes', 'Swift-4.0.6/lib/swift_required');

There. Wasn’t that easy? Remember to enable the module by adding it to your bootstrap.php file!

Read More »