Using php.activerecord with Zend Framework

Recently I’ve been learning Ruby, and at the same time Rails, with it I’ve learnt alot of good standardised ways of things to do, but without a doubt the most powerful part of Rails, for me, was ActiveRecord. So I had a search around the net and came across this fantastic little ActiveRecord implementation in PHP, called php.activerecord. In fact its so amazing I thought I would write a little guide for you to get up and running quickly with it.

We are going to create a quick and dirty model that will represent a blog post.

Please note: the version of Zend Framework when this article was written was 1.10.6

Step 1 – Download

Download php.activerecord from http://www.phpactiverecord.org (version as of writing this is 1.0)

Step 2 – Extract

Extract the file, and place it somewhere on your webserver (I put mine in my “library” directory, along with Zend Framework)

Step 3 – Bootstrap

Open your Bootstrap.php file for your application, add a protected function for initialising php.activerecord:

protected function _initActiveRecord() {
	include_once('/path/to/php-activerecord/ActiveRecord.php');
	ActiveRecord\Config::initialize(function($cfg) {
		$cfg->set_model_directory(APPLICATION_PATH.'/models');
		$cfg->set_connections(array(
			'development' => 'mysql://username:password@host/development_db_name',
			'production' => 'mysql://username:password@host/production_db_name',
		));
		$cfg->set_default_connection(APPLICATION_ENV);
	});
}

Step 4 – Database table

Set up a database table called ‘posts’ (notice the plural, this is because it holds multiple “Post” models).

CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `excerpt` text COLLATE utf8_unicode_ci,
  `content` text COLLATE utf8_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;

Step 5 – Model

Now create your model, under the “models” directory which we created during the initialisation of php.activerecord that will look something like this (notice Post is singular, this is because your model represents a singular version of what it represents)

<?php

	class Post extends ActiveRecord\Model {

	}

?>

Step 6 – Away you go!

Now you have a guide how to get up and running, take a look at the various guides available on the php.activerecord wiki to see how you would use it within your controllers.

If you find this useful, then please leave a comment and let me know. If lots of people enjoy it then I’ll create a step by step tutorial on how to get something up and running using it. Also if you have any questions then leave a comment and I’ll do my best to help wherever I can.

Posted in How to | Tagged | View Comments

How to create a Zend Controller Plugin

Ever wondered how to create a nifty little Controller Plugin for Zend Framework that will build your navigation for you and automatically send it down to the view for use in your view and layout scripts? Well here is something you might find handy. It shows how to register your controller plugin, and a basic overview of what a navigation controller plugin might look like.

Please Note: This was written when the current version of Zend Framework was 1.10.2

Step 1: Application ini

In your application config

autoloaderNamespaces[] = "My_"

Step 2: Bootstrap

In your Bootstrap file

protected function _initRegisterControllerPlugins() {
	$this->bootstrap('frontController') ;
	$front = $this->getResource('frontController') ;
	$front->registerPlugin(new My_Controller_Plugin_Navigation());
}

Step 3: Controller Plugin

Now, create a new file called “Navigation.php” under your “/path/to/lib/My/Controller/Plugin/” directory. This will be our Controller Plugin.

<?php

	class My_Controller_Plugin_Navigation extends Zend_Controller_Plugin_Abstract {

		public function preDispatch(Zend_Controller_Request_Abstract $request) {
				// Get the view, we'll need to assign the navigation to it later
			$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
			if (null === $viewRenderer->view) $viewRenderer->initView();
			$view = $viewRenderer->view;

				// Create a new Navigation Object
			$container = new Zend_Navigation();

				// Create the pages
			$pages = array(
				array(
					'label' => 'Home',
					'uri' => '/',
					'pages' => array(),
				),
				array(
					'label' => 'Page 2',
					'uri' => '/page-2',
					'pages' => array(),
				),
				array(
					'label' => 'Page 3',
					'uri' => '/page-3',
					'pages' => array(),
				),
			);

				// Set the pages to the navigation container
			$container->setPages($pages);

				// Set the active page
			$activePage = $container->findByUri($request->getRequestUri());
			$activePage->active = true;

				// Assign the navigation to the view
			$view->navigation($container);
		}

	}

?>

Step 4: Output the navigation

Now you can generate your top level navigation from within your view or layout.

<?php echo $this->navigation()->menu()->setMaxDepth(0); ?>

Hope you enjoyed this brief overview of how to create a Navigation helper. As you can see, if your structure is stored in a database table this would be really easy to modify to place it inside the Zend_Navigation object.

Posted in How to | Tagged | View Comments

Javascript CDATA

Just for my own sanity (because I always forget this for some reason).

<script type=”text/javascript”>
/* <![CDATA[ */
JAVASCRIPT CODE HERE
/* ]]> */
</script>
Posted in Code Snippets | Tagged | View Comments