Archive for the Category Development

 
 

Indexes on multiple columns in MySQL

When optimizing queries, you don’t need to be reminded that indexes play an important role.

Something to remember though: in real-world examples a multi-column index often outperforms multiple indexes.
So it can be worth changing something like:

INDEX `index1`(`column1`),
INDEX `index2`(`column2`)

to

INDEX `index1`(`column1`, `column2`)

MySQL 5.0 introduced an internal method called “Index Merge” which merges multiple indexes when querying a table (if applicable) but I haven’t seen this being used that often. On the contrarry, MySQL often picks one of the indexes and in these scenario’s it is especcialy usefull to have one multi-column index.

As always: use EXPLAIN to see what can be improved.

Redirecting in Zend Framework

While working with Zend Framework, you must have asked yourself “Is there an easy way to redirect to another controller/action?”

Well, there is. When redirecting inside a controller, the first method we think about will probably be $this->_redirect(). This is quick, but it only accepts a URL. This means you have to create a URL to a specific route yourself. Either with the $this->view->url() URL view helper (which is not a pretty sight) or by assembling a route.

Yes, there is also $this->_forward() which sends your request to another controller/action, but it also goes through all init() logic again and forwarding isn’t suited for every situation (e.g. after a form POST).

But there is a far more flexible way: the less known Redirector action helper. Actually, $this->_redirect() uses it, but it provides easier ways to redirect to a specific route:

$redirector = $this->getHelper('Redirector');
$params = array(
	'controller' => 'my-controller',
	'action' => 'my-action',
	'other-param' => 'value'
);
$redirector->gotoRoute($params, 'my-route', true);

Its usage will be self explanatory (actually the parameters are the same as the URL view helper), but I want to add a little note about the last parameter (which the URL view helper also accepts): if true, it “resets” your route, meaning all other parameters currently in your Request object are removed. This implies that when setting it to false (which is the default) parameters you have in your “current URL” will be reused (unless overwritten).
This can be particularly useful when passing search query, paging,… parameters from one page to another.

So, next time you need to redirect: pull out the Redirector action helper.

Stay informed on Zend Framework

When developing with Zend Framework, it is difficult to keep track of new versions, new components, changes to components, roadmaps,…

After “Beginning Zend Framework“, you’ll need to stay current too. You can of course read everything on the wiki, but if you use a RSS reader (if not, you really need to) add this feed.

It provides a great overview of all proposals and discussions concerning components. Even if you don’t have time to follow up on this each day (there can be a lot of comments), by looking at the titles of the posts you can have an idea of what is going on.

Beginning Zend Framework

One of the consequences of the huge popularity of Zend Framework is that everyone who is using PHP in one way or another has at least thought about using it (because it all sounds so good and, possibly, easy).

But different people need different approaches to start developing with Zend Framework. And the publishing companies that published the first books about Zend Framework (and apparently also the second wave of books) tend to forget about some groups of users. A little summary (in no way meant to offend):

Developers with a different background (another programming language)

Depending on the background, migration will not be that hard. I can imagine a .NET or Java developer easily gets the principles of a framework (and hopefully also understands OO programming). Ruby or Python will be even easier (but why on earth would they migrate?).

The average PHP developer (not already using Zend Framework of course)

If the Zend Framework manual is not sufficient for this group, there certainly are a lot of books available that eliminate all barriers. Of course look for the most recent books, since a lot of versions have been announced the last 2 years and, to be honest, some of the early books were published in a hurry to benefit from being among the first.

Dreamweaver users (often called “webdesigners“)

Yes, this group is also (willingly or unwillingly) pushed to, at least, have a look at Zend Framework. And to be honest, at the moment, I wouldn’t know how they can prevent themselves from creating something that isn’t maintainable in the years to come.
Zend Framework (or any other framework in this case) promises a lot of good things (which may lack in the current PHP snippets, Smarty templates, open source CMS,… now being used). Starting with the manual certainly is tricky. Getting a thorough grip of PHP/OO concepts may sound boring, but you need it.
As a suggestion: don’t set too high expectations and take your time. It may be useful to force yourself to get a PHP certification. Not because you need it, but because it forces you to learn/refresh the PHP basics.

Also: be honest to yourself. If you notice OO concepts don’t interest you, think about it and maybe try something else. Another lightweight framework like CodeIgniter (there is no such thing as “the best framework”) or no PHP development at all may better suit you. No one has ever been happy forcing themselves in a position doing something they don’t like to do!

Dummies

Belonging to this group is no disadvantage. Ok, you don’t have the experience of someone who has been using PHP since before the dot-com bubble burst, but at least you acknowledge this. Starting from scratch (and knowing it) can eliminate a lot of barriers.
Start by learning PHP and fairly early start with a decent PHP OO book and, again, take your time. In my opinion you don’t have to finish 5 courses and 3 books to start with Zend Framework. When learning Zend Framework (e.g. from the manual) you’ll also learn OO principles and best practices, but you need a basic knowledge first.
Also don’t expect that you can add new Zend Framework components on day 2, it will take time.

Did I forget about someone? I certainly hope this post can help as a starting point for new Zend Framework developers (or people thinking about becoming one).

Gettext translation with Google Translate

Working with gettext as your translation engine your project? In need of a rough translation of your application for a mockup/preview/…?

Paul Dixon offeres a free service that automatically translates a gettext PO file with Google Translate. I personally use gettext a lot because of its speed and ease of use (with poEdit) and this will certainly be of use.

Of course the results should not be used in a production application.

Propel version 1.4.0 released

propel

Although I must admit I didn’t notice it, less than 2 months after it was decided to create a new version for Propel, it was actually released.

Last week, on the 8th of November, the new 1.4.0 version has been released for production use. Additionally they also set up a blog to prove Propel is still very much alive.

Propel 1.4 is a backwards compatible evolution of Propel 1.3. It offers lots of bug fixes, some very interesting new features, speed enhancements, and a very simple upgrade path: rebuild your model after updating Propel, and your application works as always. Except it works better…

It all sounds to good to be true!

A first migration of a project to this new version went smoothly: remember to remove the *-classmap.php file that was generated by 1.3.0 because it has been integrated in the main config file. It’s also recommendable to delete the /map sub-folder in your models folder because 1.4.0 generates newly named *TableMap.php files.

Also good to have: full query logging (Propel 1.3 switched to PDO and lost this feature but it’s back!).

I must also admit I have never actually implemented Doctrine, but that’s also because I haven’t found a reason while using Propel.

Add Apache Ant to PDT

I’ve been looking for this for a while: how to add Apache’s build tool Ant to Eclipse when it is not available to add as a view.

When you download and run the standard PDT for example, it is not available.

The answer seems to be: install the Eclipse Java Development Tools.

This may not be the ideal way but if you want to do this through the “Install New Software…” menu, it seems to be the only one.

Just select “Work with: Galileo” (or whatever your Eclipse version is) expand “Programming Languages” and select “Eclipse Java Development”.

After the installation completes you can add Ant as a view.

Zend Server, a first impression

Zend Server

I finally tried Zend Server Community Edition as a replacement of WampServer (or XAMPP) for local development.

While Zend Server proposes to be the perfect solution for any environment, I specifically tried it as a development stack on Windows machines. The use and usefulness of course completely differ in any other scenario.

First of all (and of great importance if you ask me): it is clean, simple and easy to use from A to Z. The installer provides the right options but not too many, the dashboard (control panel) is slick and provides quick access to settings you don’t often find in any other “server packages” and last but not least it performs much better due to the use of FastCGI on Windows.

2 less-frequently used features I missed though:

  • A way to set up VirtualHosts in the dashboard.
  • A WampServer-like way to switch between PHP (and in a lesser extend Apache) versions.

In total, the fine packaging almost got me convinced to replace WampServer.

Almost… because the choice for FastCGI on Windows also has one major disadvantage: you cannot use php_value settings inside your Apache/VirtualHost configuration. I especially like this since it avoids setting include_path, display_errors, upload_max_filesize and the like inside your scripts or .htaccess (which in some setups causes it to be parsed at every request).
Doing this in Zend Server on another platform isn’t a problem, since they use the PHP Apache module instead of CGI but that means you do not have similar environments and that is the complete opposite of what we want.

So it ended with a deinstallation. On the other hand: I can really see the benefit of this package for live/production environments. With it’s included optimization tools it takes a lot of configuration hassle away.

Google results right-click FireFox reset

When you use FireFox, you might notice that the Google search results show a different URL once you click on a link.

Google has been experimenting/implementing/… this for quite some time now but apparently not in all countries/browsers/users/… simultaneously?

It is caused by some JavaScript script that re-routes the link through Google in order for them to be able to track clicks. Although I can imagine some people make a fuss about this, personally I do not care as long as the results are good.

But this becomes annoying once you right-click a link to copy it (e.g. in an e-mail): I want to send the original URL and it sounds a bit strange to track other people clicking on my search result (for some good reason it probably makes sense to Google though).

You can easily avoid this (at least in FireFox) with a Greasemonkey script. Scripts have been available for some time, but I made a simple one with jQuery that does the trick for me.

Download or install jQuery Google click tracking removal script.

Use standards

Use standards

No, you guessed wrong. This will not be the nth post about W3C standards.

Well, maybe one paragraph: while I know the use of W3C standards is favourable, the usability of your project/application should be your number one priority. Not adherence to a standard which would render your application unusable for (a part of) your audience. Every once in a while people tend to forget this. Of course no one will argue that W3C standards actually are a blessing.

But, this post is mainly about 2 simple standards:

  • The ISO 639 standard for representing languages. Please use “en” or “fr” in your database, routing, scripts,… . It is unbelievable how many times people pick something like “FR” or “F”.
    Next time, consult the ISO 639-1 list.
  • The ISO 3166 standard for representing countries. Same case: please use “US”, “FR” or “BE” or use the ISO 3166-1 list.

Additionally, use the Unicode CLDR list when working with locale’s (e.g. nl_BE).

Standards in general provide a major productivity benefit: you do not have to lose time discussing, implementing, mapping,… other peoples personal preferences (whether or not their choice can be motivated).

This also applies to coding standards/guidelines. We have probably all had (or will have) discussions about them once in our life because there are no “official” ones as far as I know. Specifically about PHP: please (yes, I am on my knees right now) use one of the major ones that are already available: e.g. Zend Framework (my preference because it is or will become the industry standard), PEAR or … . Do not create your own flavour (I do not have to explain why this does not make sense).

A hot topic at your company, project or team? Have a look at Weble Subversion hosting: you can force a list of predefined guidelines/standards by a mouse click. This will prevent SVN users from committing their code if it does not comply and, in such a case, an overview of the deviations will be displayed.