SpiffyJr's Blogaroo

Happenings of the man known as SpiffyJr

  • Home
  • About
    • Inspiration
    • Resume
  • Projects
    • Blitzaroo
    • phpRaider
    • Zend Calendar
  • Docs
    • Blitzaroo API
    • SpiffyCalendar Docs
    • SpiffyDb Docs
  • Downloads
  • Technologies
    • Doctrine ORM
    • Dojo
    • PHP
    • Zend Framework
  • Other
    • License
Twitter RSS

Zend Framework 1.11 + Doctrine 2 – Let’s play nice, part 1

Posted on November 17, 2010 by SpiffyJr
13 comments

Introducing – ZF + D2

Greetings! Welcome to my probably wrong but hopefully right series on integrating Doctrine 2 with Zend Framework! I’m a huge fan of frameworks and object relational mappers (ORM’s) like many developers. Recently (okay, maybe not so recently) I started working on Blitzaroo and immediately decided upon the Zend Framework along with Doctrine 2. I had previous experience with the Zend Framework and Doctrine 1 but I wanted to check out the hype surrounding Doctrine 2. Doctrine 1 was pain in the rear to integrate with Zend Framework but fortunately it’s pretty painless with Doctrine 2.

Getting Started

First off, let’s gather the required files. I’m going to assume you have a working Zend Framework project. If not, you can always check out the Zend Framework Quick Start tutorial.

  1. Download Doctrine 2 (BETA 4 at the time of writing)
  2. Extract Doctrine 2 to a folder within your project or PHP include path. I tend to use a vendor/ folder third-party libraries but the choice is yours. I’m going to assume you extracted to your /vendor folder
  3. Profit?

If you would prefer you can download the project from GitHub which has the above setup for you already.

Integrating the goodness

So, after three short steps you should have all the files in the appropriate places or have downloaded the sample project and can follow along. The first order of business is getting everything integrated nicely. We’re going to setup the Doctrine 2 as a re-usable Zend_Application resource plugin thanks to Matthew O’Phinney’s nifty guide.

My_Resource_Entitymanager

class My_Resource_Entitymanager extends Zend_Application_Resource_ResourceAbstract
{
        // Default array of options that can be overridden using the application.ini file.
        // e.g., resources.entityManager.autoGenerateProxyClasses = false
	protected $_options = array(
		'connection' => array(
			'driver' => 'pdo_mysql',
			'host' => 'localhost',
			'dbname' => 'dbname',
			'user' => 'root',
			'password' => ''),
		'modelDir' => '/models',
		'proxyDir' => '/proxies',
		'proxyNamespace' => 'Proxies',
		'autoGenerateProxyClasses' => true
	);

	public function init()
	{
		$options = $this->getOptions();

		$config = new \Doctrine\ORM\Configuration;
		$cache = new \Doctrine\Common\Cache\ArrayCache;
		$driverImpl = $config->newDefaultAnnotationDriver($options['modelDir']);

		$config->setMetadataCacheImpl($cache);
		$config->setQueryCacheImpl($cache);
		$config->setProxyDir($options['proxyDir']);
		$config->setProxyNamespace($options['proxyNamespace']);
		$config->setAutoGenerateProxyClasses($options['autoGenerateProxyClasses']);
		$config->setMetadataDriverImpl($driverImpl);

		$em = \Doctrine\ORM\EntityManager::create($options['connection'], $config);

		return $em;
	}
}

Wait a minute!

I know, I know, you’re probably thinking “How do we access the entity manager from the rest of the project?” Easy! Just access it like any other resource.

// To access in a controller use getInvokeArg() like so
public function indexAction()
{
    $em = $this->getInvokeArg('bootstrap')->getResource('entityManager');
}

// To access elsewhere use the Zend_Controller_Front singleton
public function myRandomMethodSomewhere()
{
  $em = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('entityManager');
}

That’s all folks!

Well, not really, but for now. You can check back later for the other parts as I get them finished (and decide what to write). Cheers!

GD Star Rating
loading...
Categories: Random

Rendering Dojo Stylesheets with a view helper

Posted on October 26, 2010 by SpiffyJr
2 comments

I’ve been fervently coding Blitzaroo the passed week and I ran into a rather annoying issue when using AJAX with the Zend Framework and Dojo. Take, for example, using AjaxContext to load content for a dijit.layout.TabContainer that houses several dijit.layout.ContentPane(s).

$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContexts(array(
    'overview' => 'html',
    'events' => 'html',
    'members' => 'html',
    'recruits' => 'html'))->initContext();

Now then, when the getHref() from a ContentPane is called they load the Ajax data which, in this case, happens to be a dojox.grid.DataGrid. The grid renders just fine without Ajax but when Ajax is used ZF fails to load the proper Stylesheets which need to be loaded manually. I thought, no big deal, I’ll just hit up $this->dojo()->{some method to spit out stylesheets} and to my dismay found that the method is protected. Why? I’m not quite sure. I went ahead and wrote a view helper to fix the problem which is merely a copy/paste of the _renderStylesheet() method but without requiring the Dojo container object. If there is another way around the issue I’m all ears!

class My_View_Helper_DojoStylesheets extends Zend_View_Helper_Abstract
{
	public function dojoStylesheets()
	{
		$dojo = $this->view->dojo();
		$isXhtml = $this->view->doctype()->isXhtml();

		if ($dojo->useCdn()) {
			$base = $dojo->getCdnBase() . $dojo->getCdnVersion();
		} else {
			$base = $dojo->_getLocalRelativePath();
		}

		$registeredStylesheets = $dojo->getStylesheetModules();
		foreach ($registeredStylesheets as $stylesheet) {
			$themeName = substr($stylesheet, strrpos($stylesheet, '.') + 1);
			$stylesheet = str_replace('.', '/', $stylesheet);
			$stylesheets[] = $base . '/' . $stylesheet . '/' . $themeName . '.css';
		}

		foreach ($dojo->getStylesheets() as $stylesheet) {
			$stylesheets[] = $stylesheet;
		}

		if ($dojo->registerDojoStylesheet()) {
			$stylesheets[] = $base . '/dojo/resources/dojo.css';
		}

		if (empty($stylesheets)) {
			return '';
		}

		array_reverse($stylesheets);
		$style = '<style type="text/css">' . PHP_EOL . (($isXhtml) ? '<!--' : '<!--') . PHP_EOL;
		foreach ($stylesheets as $stylesheet) {
			$style .= '    @import "' . $stylesheet . '";' . PHP_EOL;
		}
		$style .= (($isXhtml) ? '-->' : '-->') . PHP_EOL . '</style>';

		return $style;
	}
}

Now my {action}.ajax.phtml script is nice and happy.

// call the regular members action page - no need to duplicate code!
echo $this->render('team-manager/members.phtml');

// inlineScript contains all the formatters for the grid
echo $this->inlineScript();

// New view helper to include the data grid CSS files generated by my DataGrid view helper
echo $this->dojoStylesheets();
GD Star Rating
loading...
Categories: Blitzaroo, Dojo, PHP, View Helpers, Zend Framework | Tags: Ajax, api, Blitzaroo, Dojo, Dojo Toolkit, JavaScript, php, Programming, Scripts, Style Sheets, zend, Zend Framework

Google Code project online

Posted on March 18, 2010 by SpiffyJr
2 comments

I finally created a Google Code project for all my source code. You can find it at http://bit.ly/bgVXTq.

GD Star Rating
loading...
Categories: Dojo, PHP, Projects, Technologies, Zend Framework
Page 3 of 1312345...10...»Last »
  • Recent Posts

    • Keep your Git fork clean
    • Get started with Zend Framework 2 modules!
    • More Doctrine 2 and Zend Framework integration goodies!
    • Super sexy URLs with ZF and the joy of controller plugins!
    • Formatting your API to work with dojox.data.JsonRestStore (#dojo)
  • Categories

    • Other
      • Random
    • Projects
      • Blitzaroo
      • phpRaider
      • SpiffyDb
      • View Helpers
      • Zend Calendar
    • SpiffyJr
    • Technologies
      • Dojo
      • PHP
      • Zend Framework
  • Tag Cloud

      Ajax Algorithm api Blitzaroo Blog BSD licenses calendar Cascading Style Sheets CSS database Data Formats Dojo Dojo Toolkit event Framework game Google HTML HTML element JavaScript JQuery json Languages mapper Marketing mmorpg model php Programming Projects Scripts Source code Style sheet Style Sheets Twitter Website zend Zend Framework
  • Archives

    • December 2011
    • November 2011
    • July 2011
    • April 2011
    • March 2011
    • December 2010
    • November 2010
    • October 2010
    • March 2010
    • February 2010
    • November 2009
    • October 2009
    • September 2009
© SpiffyJr's Blogaroo. Proudly Powered by WordPress | Nest Theme by YChong