Feed items

OpenX and HostGator shared hosting

This post is totally unrelated to CakePHP, but oh well!
As you might have noticed, OpenX does not work on HostGator’s default shared hosting. Every ad you post on your website through OpenX will be viewable, but clicking the ad will lead to a 403 forbidden page.
It has something to do with HostGator Apache’s default configuration on shared hosting. Globally, it is because of some configuration of mod_security, but I don’t know what it is exactly. I found out that you can’t turn off mod_security within the .htaccess. Thanksfully, the tech team will fix you up through the live support within minutes.

Common mistakes when working with accentuated characters

Working with accentuated characters in CakePHP can definitely be a pain if you’re unfamiliar with a few basic guidelines.
All your files must be encoded in UTF-8
Make sure all your files containing accentuated characters are encoded in UTF-8 (no BOM or it’ll screw everything up!). If your file does not have the correct encoding, any string that contains an accentuated character will just be considered as empty, which can be frustrating to debug.
Database connection must use UTF-8 encoding
This will prevent elements from the database to be considered as empty.

Model “X” with multiple associations leading to Model “Y”

This seems like a fairly common case, but it might not be obvious for everyone. While the title is not totally clear, let’s use an exemple to show where the problem lies.
Let’s say we have a Message model, which has the following fields : id, sender, receiver, created, title, text. We’d like our sender and receiver field to link with our User model, so from and to will contain a user id. If we were to follow CakePHP’s naming convention, we’d have two user_id fields, which is not going to work for obvious reasons. So this is where we have to break CakePHP’s naming convention, which can lead to some confusion. Fear not, the solution is quite simple!
In order for this case to work, all we have to do is change the key in our association :

Sort search results by relevance

When creating a search function for your website, it is fairly important to sort results by relevance. I had to do this last week, so I might as well share how I did it.
I wanted to search the News model with simple keywords. Here’s my News table :

Redirecting to the proper page after actions

A couple days ago, a client I currently work for found a little annoyance with cakePHP default’s behaviour of redirecting to the index action after a delete or edit action, thus ignoring the page you were on.
Indeed, redirecting to $this->referer wouldn’t work in this case because the data is posted to the same action, therefore $this->referer = $this->here. The solution is quite simple, add this code to your action: