Pragmatism in the real world

Displaying the generated SQL from a ZendDbSql object

If you use ZendDbSql to generate your SQL, then it's useful to find out what the generated SQL looks like. Consider code like this: public function fetchAllWithTitleSince($title, $since) { $sql = new Sql($this->dbAdapter); $select = $sql->select(); $select->from($this->tableName); $select->columns(array('id', 'title', 'url', 'date_updated')); $select->where->like('title', "%$title%"); $select->where->greaterThanOrEqualTo('date_created', date('Y-m-d', strtotime($since))); $statement = $this->dbAdapter->createStatement(); $select->prepareStatement($this->dbAdapter, $statement); return $statement->execute(); } To find out what the generated SQL will look like, you can use the $select's getSqlString() method: $select->getSqlString(); For me, this… continue reading.

IN and Zend\Db\Sql's where()

This is a short note to myself. Zend\Db\Sql objects allow you to do this: $id = 2; $select->where(array('id' => $id)); which generates the (My)SQL: WHERE `id` = '2' If you want the SQL generated to use the IN operator, then just pass in an array: $idList = array(1, 3, 4); $select->where(array('id' => $idList)); which generates: WHERE `id` IN ('1', '3', '4') This obviously also works for ZendDbSql's update() as well as select().

Caching your ZF2 merged configuration

Zend Framework 2's ModuleManager has the ability to cache the merged configuration information for your application. This is very useful as it allows you to separate out your configuration within the config/autoload directory into logical files without worrying about the performance implications of lots of files. Enabling caching is simply a case of setting these configuration keys in config/application.config.php within the module_listener_options section: 'module_listener_options' => array( 'config_cache_enabled' => true, 'module_map_cache_enabled' => true, 'cache_dir' => 'data/cache/',… continue reading.

Injecting configuration into a ZF2 controller

One thing you may find yourself needing to do is access configuration information in a controller or service class. The easiest way to do this is to use the ServiceManger's initialiser feature. This allows you to write one piece of injection code that can be applied to multiple objects. It's easier to show this in action! Let's assume that we have this configuration file: config/autoload/global.php: return array( 'application' => array( 'setting_1' => 234, ) );… continue reading.

Simple logging of ZF2 exceptions

I recently had a problem with a ZF2 based website where users were reporting seeing the error page displayed, but I couldn't reproduce in testing. To find this problem I decided to log every exception to a file so I could then go back and work out what was happening. In a standard ZF2 application, the easiest way to do this is to add a listener to the 'dispatch.error' event and log using ZendLog. To… continue reading.

Changing the format of a ZendForm DateTime element

If you want to change the format of the value of a DateTime element, the easiest way to do this in your Form class is to do this: $this->add(array( 'name' => 'next_appointment', 'type' => 'ZendFormElementDateTime', 'options' => array( 'label' => 'Next callback time', ), 'attributes' => array( 'min' => '1 Jan 2013, 00:00', ), )); $this->get('next_appointment')->setFormat('j M Y, H:i'); The two things to note: You can't set the format within the array – it has… continue reading.

Thoughts on module directory structure

I've been working on a Zend Framework 2 module within a larger project that doesn't have that many PHP class files. Specifically, it has a controller, a mapper, an entity, a service and a form. As a result, the traditional Zend Framework 2 directory structure for the Account module looks like this (with class names in brackets): module/ Account/ config/ src/ Account/ Controller/ CaseController.php (Account\Controller\CaseController) Entity/ CaseEntity.php (Account\Entity\CaseEntity) Form/ CaseForm.php (Account\Form\CaseForm) Mapper/ CaseMapper.php (Account\Mapper\CaseMapper) Service/… continue reading.

Using ZendSession

This is a quick note on how to use ZendSession. Although the component name is ZendSession, you actually interact with ZendSessionContainer to store and retrieve session data: use ZendSessionContainer; $session = new Container('SomeKeyName'); ZendSessionContainer's constructor takes a string argument which is the name for this container ('SomeKeyName' in this case). It's optional and if you don't set it, then it is set to 'Default'. The name allows you to use the same session keys in… continue reading.

Integrating BjyAuthorize with ZendNavigation

If you are using BjyAuthorize for ACL configuration and want to use ZendNavigation's ZendAcl integration features, then you need to set the Acl and Role information into ZendNavigation. The easiest way to do this is to add the following to ApplicationModule::onBoostrap(): $sm = $e->getApplication()->getServiceManager(); // Add ACL information to the Navigation view helper $authorize = $sm->get('BjyAuthorizeServiceAuthorize'); $acl = $authorize->getAcl(); $role = $authorize->getIdentity(); ZendViewHelperNavigation::setDefaultAcl($acl); ZendViewHelperNavigation::setDefaultRole($role); This assumes that you've set up BjyAuthorize with some resources and… continue reading.

Introducing AkrabatSession

One of the requirements for a new app that I'm writing is that it has a specific session name. In Zend Framework 2, this is done by creating a SessionManager with the correct configuration and then setting the default manager on the Session Container: use ZendSessionConfigSessionConfig; use ZendSessionSessionManager; use ZendSessionContainer; $sessionConfig = new SessionConfig(); $sessionConfig->setOptions(array('name'=>'MY_SESSION_NAME'); $sessionManager = new SessionManager($config); Container::setDefaultManager($sessionManager); Obviously, I need to be able to configure the name (and potentially other session configuration… continue reading.