I was working on an application which was using alot of number-formatting using Zend_Currency. I was using a view helper for this. The view helper looked like this:
class Sterc_Helper_ToEuro extends Zend_Controller_Action_Helper_Abstract {
public function direct($amount) {
require_once('Zend_Currency');
$currency = new Zend_Currency('nl_NL', 'EUR');
return $currency->toCurrency($amount);
}
}
There was a certain page which called this view helper about 10 times and the whole page took 12 seconds to load. I was initially looking at my MySQL queries, which caused some problems in this application before, but I was wrong. It was the view helper, containing Zend_Currency, which caused the problem.
To fix this problem, there is a caching option, which is documented here in the Zend Framework Manual.
I was about to test this, when I thought of another really easy fix for this issue: use PHP’s number_format. Why use an entire Zend Framework class when a PHP built-in function is available?
My new view helper looks like this:
class Sterc_Helper_ToEuro extends Zend_Controller_Action_Helper_Abstract {
public function direct($amount) {
return '€ ' . number_format($amount, 2, ',', '.');
}
}
My page load time dropped from 12 seconds, to 0.13 seconds. Not bad!
I’ve been using Subversion for some years now for some internal projects at Sterc. Because of my recent switch to mac, TortoiseSVN wasn’t available anymore for me. I tried several subversion clients and ended up with svnX, Textmate’s internal subversion and Zend Studio’s internal subversion.
I mostly used svnX but it had some downfalls:
- no svn:ignore
- unintuitive interface
- no more development in the past year (or I’ve missed something…)
I stumpled upon an article on John Mertic’s blog today about Versions. Looking at Versions’ website I already got excited! A well designed site, a bit to much text on one page though, but way better than other subversion clients’ sites. I downloaded the demo, installed it, added a repository and everything works like a charm, very intuitive!
I will be using the Versions demo for the next 21 days and we’ll see if I will stay this excited about it. I’ll keep you posted.
I was working on an application where I needed all unique brands from a mysql table to populate a HTML select box. I have no idea how to do that quickly with Zend_Table, so I found out you can just pass on your own SQL if you want.
A simple “SELECT DISTINCT brand FROM products ORDER BY brand ASC” will do in these cases. It would even be better if I had a seperate brands table, so I wouldn’t have this problem at all. But since this was a quick and dirty job, there was no brands table ;)
Anyway, these lines will do the job (where $db is an instance of your Db adapter):
$statement = $db->query('SELECT DISTINCT brand FROM products ORDER BY brand ASC');
$this->view->brands = $statement->fetchAll();
Use foreach() in your view to display the list:
echo ' ';
And that’s it!
Ik zat vandaag met het volgende probleem:
Oplossing:
In principe is een view helper gewoon een class. Een class kun je gewoon aanroepen en uitvoeren vanuit bijvoorbeeld de view helper “showMenu”:
Zend_Loader::loadClass('Sterc_View_Helper_MakeUrl');
$url = new Sterc_View_Helper_MakeUrl();
$link = $url->makeUrl($linkId, 'page');
Kleine kanttekening hierbij is: De view helper staat dus hier: ‘/library/Sterc/View/Helper/MakeUrl.php’.
Zo simpel is het dus: 3 regels code, in plaats van de hele functie makeUrl in bijvoorbeeld de view helper ShowMenu te plakken.
Textmate is mijn meest gebruikte editor, sinds ik een Macbook heb. Sinds gisteren gebruik ik nu ook mijn zelfgemaakte theme. Je kan hem hier bekijken.
Het thema is geschikt voor PHP, HTML (met PHP) en JavaScript en zal ongetwijfeld worden gewijzigd om beter te werken met Zend Framework en jQuery, omdat ik dat dagelijks gebruik.