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->columns(array('id', 'title', 'url', 'date_updated'));
$select->where->greaterThanOrEqualTo('date_created', date('Y-m-d', strtotime($since)));
$statement = $this->dbAdapter->createStatement();
To find out what the generated SQL will look like, you can use the $select's getSqlString() method:
For me, this results in lots of warnings along the lines of: This is less than helpful, so to avoid the warnings, you need to supply the correct platform information to the… continue reading.
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:
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));
WHERE `id` IN ('1', '3', '4')
This obviously also works for ZendDbSql's update() as well as select().
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/',
// other keys go here (e.g. module_paths & config_glob_paths)
This then creates the cache files data/cache/module-classmap-cache.php and data/cache/module-config-cache.php and you're… continue reading.
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:
'application' => array(
'setting_1' => 234,
That is, we have a key called 'application' that… continue reading.
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.
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:
'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 to be via a setFormat() call. If you change the format, you must set the min attribute in the same format, as otherwise it will try to set it with… continue reading.
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):
That's a lot of directories for not many files! As a result, I decided to flatten it… continue reading.
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:
$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 different containers. To set data… continue reading.
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();
This assumes that you've set up BjyAuthorize with some resources and rules. For example, in my config/autoload/bjyauthorize.global.php, I have a 'bug' resource and have a rule that allows the reporter role access to… continue reading.
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:
$sessionConfig = new SessionConfig();
$sessionManager = new SessionManager($config);
Obviously, I need to be able to configure the name (and potentially other session configuration options) from my config/autoload/global.php file and this is a generically useful requirement, so I created… continue reading.