Category Archives: zend framework

ZF2: how to override configuration (for testing purposes for instance)

My requirement was simple: be able to use a dedicated database for testing purposes. Next question was: how do I say to ZF2 to use this database when testing and another one when not testing. A quick solution would be to hardcode this in the config file itself based on an environment variable in the same way it was one with ZF1. I wanted to have smoother integration with ZF2. After looking at the module, here is what I found: Zend\Mvc\Application::init() retrieves module_manager from service_manager Zend\ServiceManager\ServiceManager::get() returns module_manager after instanciating it using Zend\Mvc\Service\ModuleManagerFactory Zend\Mvc\Service\ModuleManagerFactory set up listeners on the module-manager […]

ZF2: “Row count is not available in unbuffered result sets.” [solved]

When using db module of ZF2 framework, I faced the following error message: Fatal error: Uncaught exception ‘Zend\Db\Adapter\Exception\RuntimeException’ with message ‘Row count is not available in unbuffered result sets.’ in library/ZendFramework-2.0.0rc7/library/Zend/Db/Adapter/Driver/Mysqli/Result.php:304 By default, buffering of results is not activated. There is a simple option named ‘buffer_results’ which needs to be set to true in the configuration file. Editing /config/autoload/global.php to add the above mentionned option saved the problem: return array( ‘db’ => array( ‘options’ => array( ‘buffer_results’ => true, ), ), ); sources  

ZF2: how to use Zend\ModuleManager without full mvc configuration / how to properly configure Zend\ServiceManager

I am trying to get used to ZF2 and want to use some parts selectively. In this case, I want to benefit from Zend\ModuleManager without using full out-of-the-box Zend\Mvc\Application approach. When trying to instanciate a Zend\ModuleManager object, I ran into the following error: Fatal error: Call to a member function attach() on a non-object in library/ZendFramework-2.0.0rc7/library/Zend/ModuleManager/Listener/LocatorRegistrationListener.php on line 70 Faulty line is the following: $events        = $moduleManager->getEventManager()->getSharedManager(); $events->attach(‘Zend\Mvc\Application’, ‘bootstrap’, function ($e) use ($moduleManager) { I did not specify any specific event manager when instanciating the ModuleManager, and as such default one was created with no sharedEventManager. This […]

my first ZF2 application (ZF2 MVC tutorial debugged)

At time of writing, ZF2 version beta4 is out and there is no real formal documentation on how to write a ZF2 based application. I tried to create my first application (now called “module”) using ZF2, following this documentation: Unfortunately, this did not work straight out of the box. It seems that the tutorial was designed for a version lower than beta4 and is not working anymore. Below is a summary of the issues I faced following the tutorial and how they were solved. autoload.php Below error was found Warning: include_once(vendor/autoload.php): failed to open stream: No such file or […]

Zend_Form: add custom validators via .ini config

To add custom validators to a Zend_Form via .ini form configuration file, proceed as follow: identify the path of your custom validators (ex: /library/validators/) identify your custom validator prefix (ex: all my custom validator classes start with ‘Validate_’) open form .ini configuration file and add the following two lines directly at root level (ie. do NOT attach them to an element but rather to the form itself => at very top of the file): elementPrefixPath.validate.prefix = “<YOUR VALIDATORS PREFIX>” elementPrefixPath.validate.path = “<PATH TO VALIDATORS>” In my case: elementPrefixPath.validate.prefix = “Validate” elementPrefixPath.validate.path = “validators/” still in .ini file, simply add your […]

Configure Zend_Mail to dump emails into files rather than sending emails

It is possible to configure Zend_Mail to dump emails into files rather than send emails, which can be really great when developing the application! How to do it? In you configs/application.ini file, configure the mail resource as follows: [development : production] resources.mail.transport.type = “file” resources.mail.transport.path = APPLICATION_PATH “/../logs/dumped_emails/” Make sure to customize the destination path to the folder you want. From now on, all emails will be dumped into files within the resources.mail.transport.path instead of being sent as emails. Note: I am using Zend Framework 1.11 sources

Zend_Log + Zend_Mail = Fatal error: Exception thrown without a stack frame in Unknown on line 0

I am developing an app under Zend Framework 1.11 and found out that the combination of Zend_Log + Zend_Mail can lead to the obscure error message: Fatal error: Exception thrown without a stack frame in Unknown on line 0 Why does this error happens? An error is thrown when Zend_Mail fails to send an email (which can be the case when you develop in localhost for instance). If you use Zend_Log to log your errors (which you should definitely do) and log errors via mail too (via Zend_Log_Writer_Mail which relies on Zend_Mail), the above mentioned error is the output due […]

Zend Framework & PHPUnit: getting it to work

When trying to have unit-tests work with Zend_Framework using PHPUnit, I ran into the following error message: PHP Fatal error:  Uncaught exception ‘PHPUnit_Framework_Exception’ with message ‘Neither “Library Test Suite.php” nor “Library Test Suite.php” could be opened.’ in /usr/share/php/PHPUnit/Util/Skeleton/Test.php:100 To solve this issue, you need to: encapsulate default phpunit.xml content into a <testsuites> node (so that hierarchy becomes: <phpunit …><testsuites><testsuite>…</testsuite><testsuite>…</testsuite></testsuites>…</phpnunit> change <testsuite name=”Library Test Suite”> into <testsuite name=”LibraryTestSuite”> at the root of the ‘tests’ folder, create the file LibrarTestSuite.php with following content: <?php require_once ‘PHPUnit/Framework/TestSuite.php’; class LibraryTestSuite extends PHPUnit_Framework_TestSuite { public static function suite() { $suite = new PHPUnit_Framework_TestSuite(‘LibraryTestSuite’); $suite->addTestSuite(‘LibraryTestSuite’); return […]

Zend-studio debugger not working

When I installed Zend-studio and tried to debug an application, nothing happened. After looking at apache logs, I found the following entry: [Zend Debugger] Debugger compile handler overriden, will not be active in this request Thanks to the following post, I found out that the problem was a conflict between php xdebug module and zend-studio debugger. After removing xdebug (apt-get remove php5-xdebug), debugging via zend-studio worked like a charm! sources

How to use/write resource plugins with Zend_Application

(apply to ZF v1.10) You have two types of resources: – class resources (those methods that you define in your Bootstrap class and that starts with ‘_init’) – plugin resources (those that you define externally and can trigger via application.ini file) How do you automatically load an external resource from application.ini file? This is pretty simple indeed. You only have to declare your resource in your config file as follow: “resource.<resource_name> = true” note: we added ” = true” because otherwise our line would have been ignored since no value was defined to our <resource_name> variable. In next section, we […]