Tag Archives: controller

How to Dump the Generated Zend_Db SQL Query

The Typical PHP Approach

Typically a PHP programmer will write his SQL query as a string and will execute it via mysql_query.

$sql = "SELECT * FROM my_table";
$resource = mysql_query($sql);

So eventually when you want to dump this “complex” query, or whatever query there is, you can simply “echo” it and see what’s its syntax.

// this query is WRONG because of the where clause
$sql = "SELECT * FROM my_table WHERE id = ";
 
// dump and debug the wrong query
die($sql);
 
// this line won't be executed
$resource = mysql_query($sql);

So far so good, but things appear to be a bit different when you start to work with Zend Framework. Higher levels of abstraction come with slightly more difficult ways to dump (debug) your SQL queries.

OK you’ve two options. Using Zend_Db_Select or … not.
Continue reading How to Dump the Generated Zend_Db SQL Query

Models in Zend Framework – Initialize All Methods with init()

In the Zend Framework’s documentation there are lots of examples how you can initialize all the actions in a given controller – by simply adding the init() public method in the controller’s code:

<?php
 
class IndexController extends Zend_Controller_Action
{
	public function init()
	{
		echo 'foo';	
	}	
 
	public function indexAction()
	{
		// first the 'foo' string will be printed
		echo 'bar';	
	}
}

But did you know that you can do the same thing with any model in ZF? However you can setup a cache for every method or something else, but definitely it will execute for every method:

<?php
 
class MyModel
{
	public function init()
	{
		// prepare the cache setup
	}	
 
	public function readAll()
	{
		// the cache is already setup
		$sql = '...';
		// ...
	}
}

Of course that means that directly calling the readAll() function the init() method is called also – automatically.

Conclusion

There are good and bad parts about this. You’ll have this code executed for every method and if you have twenty of them and the init() method is practically used for only a couple of the member functions – than this will be useless.

Setting Up Zend Framework with Modules

Typical Zend App

Typically you’ve one module in your Zend App – the default one. In the basic installation of the framework, you put all the controllers, models and views directly in the application folder, as described below.

/application
	- /controllers
		- /IndexController.php
	- /models
		- /MyModel.php
	- /views
		- /scripts
			- /index/index.phtml
/library
	- /Zend
/public_html
	- /images
	- /scripts

Bigger Apps – More Code

When the application becomes bigger and bigger the controller, models and views/scripts directories contain more and more files. That’s a bit odd, because it becomes difficult to maintain, and than the modules come in hand.

Modules in a Zend App

When it comes to setting up modular Zend App there are tons of articles in the web, but let me show you a simple directory layout and … sample code that sets up the framework.

/application
	- /modules
		+ /admin
			- /controllers
				- /IndexController.php
			- /views
				- /scripts
					- /index/index.phtml
		+ /default
			- /controllers
				- /IndexController.php
			- /views
				- /scripts
					- /index/index.phtml
	- /models
/library
	- /Zend
/public_html
	- /images
	- /scripts

Source

Simply add this into the bootstrap:

$frontController->addModuleDirectory(APPLICATION_PATH . '/modules');

Setting Up Global Cache in Zend Framework

In a large scale web application, especially based on Zend Framework, there are lot’s of components that support built in cache support. Such are Zend_Db, Zend_Translate, Zend_Date, etc. Also you may need cache support wherever in the app, so my advice is to setup a cache instance in the “beginning”, into the bootstrap.php or even better – into a front controller plugin, and to store it into the Zend_Registry. Thus you’ve to change only the lifetime for specific needs:

<?php
 
class CacheInit extends Zend_Controller_Plugin_Abstract
{
    public function __construct()
    {
        $frontendOptions = array(
            'automatic_serialization' => true,
            'lifetime' => 60
        );
 
        $backendOptions  = array(
            'cache_dir' => realpath(APPLICATION_PATH . '/../cache')
        );
 
        $cache = Zend_Cache::factory('Core',
                                     'File',
                                     $frontendOptions,
                                     $backendOptions);
 
        Zend_Registry::set('cache', $cache);
    }
}

Than add it as a front controller plugin:

// cache plugin
$frontController->registerPlugin(new CacheInit());

Zend Framework: Connect MySQL

There are so many tutorials about that, but let me add it one more time. You’ve to use the PDO_MYSQL adapter:

// where params include adapter, host
// username, password and dbname
$db = new Zend_Db_Adapter_Pdo_Mysql($params);

and set the default adapter

Zend_Db_Table::setDefaultAdapter($db);

That can be done in the bootstrap or into a front controller plugin!