Splitting up my Beanstalk Repository

June 30, 2011 – 10:51 am

Boy, it’s been a while since my last post (being busy is good I guess), but to get back into it I thought I share a nice moment from yesterday where something “just worked”. Yes, those moments are that rare! :)

When I started my first repository at Beanstalk almost 2 years ago I decided to keep all my client projects in the same repository, mostly for convenience and cost reasons as Beanstalk’s account levels are capped by number of repositories and I wanted to stick with a small (cheap) plan (who wouldn’t ;). So far it’s been a great ride and the one-repo approach has not caused any problems.

Well, yesterday came that day where one of my clients asked to play with his code.. I had dreaded that day a bit, because I thought that would mean giving access to other client’s code as well, seeing that Beanstalk doesn’t offer the same permissions-granularity that a self-hosted repo would offer (which is fine, their service is voluntarily simple and therefore solid). Now, after looking around for a little, and discovering Beanstalk’s export/import functionality it turned out I wasn’t in bad shape after all.. I simply exported the entire client repository to my laptop and ran it through the svndumfilter command to filter all file modifications pertaining to said client. All the required commands I found here: http://blogs.nuxeo.com/dev/2006/04/dump-load-svn-repositories-using-svnadmin-svndumpfilter.html (thanks much), and after this I had a nice small .svndump file that I could re-import into a new Beanstalk repo. Voila, another happy customer.

Filed under: Rnadom Sftuf — by Richtermeister

Elegant Asset Handling - my first Symfony Plugin “teUrlToAssetPlugin”

August 25, 2009 – 10:17 am

Most websites I work on these days have some sort of header image on almost every page - an optional “banner” if you will, that adds to the general feel of the current section of the site. Since most of these sites as also managed through a CMS setup (symfony, sfDynamicCMS + custom stuff) it is a requirement to be able to adjust these images on a per-page level, even though most pages share the same image within their section..
In short, we often have a need for flexible customization without putting the user through the nightmare of picking a picture through the admin interface every time they want to create a page.

The solution we came up with is rather simple, yet so elegant that we found it to work in all our sites - we use a storage convention based on the page URL.

For example, let’s take a page with the following url:


where “sports” and “outdoors” are categories, and “hiking” is a specific activity.

The required flexibility now is that the we wanted to be able to have a base-banner for the “sports” section, be able to (optionally) override this for each sub-category, and for each specific activity. As you can see, mapping the url to a directory in the webroot and working backwards would yield just that nice cascade.

Say for example we needed a banner on said page, we would want to look for the following images (in this order):


Whichever picture is found first, wins.

We add the “banner” directory to be able to pull the same trick for other assets.. maybe a product manual, or a particular flash file for the footer? Never limit your options.. ;)

Now, the code that does this cascading lookup based on url is fairly trivial, but I wanted the whole thing to be very intuitive and simple for our front-end guys, so that they can easily use this functionality from within templates. Turns out this is a perfect case for the ArrayAccess interface, which allows you to access objects via array syntax.

The resulting template code looks as simple as:

$image = $assets["banner"];

and you we even let you specify the file type via an optional extension (defaults to JPG):

$image = $assets["banner.png"];
$document = $assets["manual.pdf"];
$audio = $assets["music.swf"];

As you can see, this setup makes it very easy to distribute general and specific assets throughout your site and administering the files via a wysiwyg or standalone asset manager - without cluttering up the admin area.

Hope you’ll find this as time-saving and elegant as I find it.

Have a great day,

Filed under: Symfony — Tags: , — by Richtermeister

PHP Error: Trying to clone an uncloneable object of class ReflectionClass

August 11, 2009 – 7:35 am

I’ve recently run into this error when deploying symfony applications:

“Trying to clone an uncloneable object of class ReflectionClass”.

What’s causing this is a relatively common setting in php.ini, called “zend.ze1_compatibility_mode”, which enables php4 compatibility mode in php5. Specifically it affects a couple of object handling conventions (empty objects cast to FALSE, and - oh this is my favorite - objects are passed by value instead of by reference.. can you imagine how much fun debugging your php5 application would be if this setting didn’t actually save you by breaking things altogether?) Needless to say some hosts like to keep this turned on to me “more compatible” with the past. Also needless to say I hate some hosts.

The way around it is either changing the php.ini setting to:


or disabling it at runtime, before the application bootstraps, via:

ini_set("zend.ze1_compatibility_mode", "off");

Hope it helps someone.

Filed under: Rnadom Sftuf — Tags: — by Richtermeister

Symfony 1.2 Admin with custom primary key

April 27, 2009 – 10:40 pm

Playing with the Symfony 1.2 Admin Generator I noticed that “out-of-the-box” it only likes to play with integer primary keys (ideally based on auto-increment columns I assume). Now, there are cases where you want to use primary keys that aren’t neccessarily numeric, for example countries or languages, where you could use the corresponding ISO codes as id.

When trying this with the new admin generator, all looks fine initially (”list” action works, “new” action works), but when you try to edit a record, which happens automatically after creating one, you will get a “404 Page not found” error. Looking at the logs you’ll see that Symfony interprets the non-numeric id as action name, and matches it with the :module/:action route. Of course this action doesn’t exist, resulting in above-mentioned error.

What’s going on here? Turns out, the culprit is the object routing class that the admin generator uses for generated modules. In my case the responsible class is called “sfPropelRouteCollection”, and it seems to expect the id column to be numeric, judging by it’s default setting for the requirements:


In above expression the d is regulatory speak for “a numeric character”, and the + means “one or more”. Luckily for us, all it takes is changing this requirement setting to make the generated modules work with non-numeric primary keys as well! The pattern that worked best for me is this:


Translated this means “either a numeric character or a letter character”, and “one or more” of those. Depending if you have special characters in your primary key (you really shouldn’t), you may have to massage this pattern a little, but take care to watch for “/” and “.” since those are used by Symfony’s routing to tokenize the url request.

The place to put this updated requirements is the routing.yml file, where it would look somewhat like this:

  class: sfPropelRouteCollection
    model:               Countries
    module:             countries
    prefix_path:    countries
    column:              id
    with_wildcard_routes: true
  requirements:  { id: "[dw]+" }

Watch out that the requirements entry is on the same level as class and options.

Needless to say I was very relieved when I found that this is less of a bug than a conventions issue, and that the admin generator didn’t completely break down on such a trivial requirement.

Happy custom Pk’ing!

Filed under: Symfony — Tags: — by Richtermeister

Secure Certificates and missing www

– 10:00 pm

Thanks to forgiving server-configurations it usually doesn’t matter whether you access a website via the fully qualified URL including “www” or without it - requests to http://codemassacre.com and www.codemassacre.com are taking you to the same place. After all you want to make it as easy as possible for people to access your website, so being forgiving is important.

However, as soon as you try to secure your site with help of a secure certificate and the corresponding https protocol, you need to pay closer attention, and, ultimately, make a choice as to which domain name the certificate applies to, because it will be either with www, or without.

Specifically, requesting https://some-domain.com for a site that only has a certificate for https://www.some-domain.com will cause your browser to raise a warning about an invalid certificate and block the site - certainly not a trust-invoking impression to give a potential customer! (unfortunately I am speaking first hand here, hehehe ;)

So, long story short, here’s an .htaccess entry that forces all requests to your site to have a www at the beginning:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^some-domain.com
RewriteRule (.*) http://www.some-domain.com/$1 [R=301,L]

What’s happening here is that  we’re watching the HTTP_HOST server variable for anything that starts with “some-domain.com”. The ^ signifies the beginning of a string. If this condition is fulfilled, we rewrite any request (.*) to the fully qualified url, and we append whatever was matched by the (.*) part - which goes into the $1 variable. Lastly, we set the R=301 flag to tell the client browsers that this redirect is permanent. The L is for good taste to make sure this is the last rule applied to this request (no point in applying any other rules..).

Now requests to https://some-domain.com will be redirected to https://www.some-domain.com, the certificate matches, and the browser is happy - and so is whoever visited your site, because as we all know “ignorance is bliss” ;)

Filed under: Rnadom Sftuf — Tags: , , — by Richtermeister

Converting MS-SQL to MySql (from .bak file or otherwise)

March 17, 2009 – 12:52 pm

What to expect:

Using this methodology, you’ll be able to move table definitions and the data contained within those tables. However, you will NOT move Views, Stored Procedures, Triggers, etc.

Views arrive as simple tables, so be prepared to recreate them by hand. Procedures don’t arrive at all, so same “manuality” applies.

These are the steps to convert an MS-SQL database to MySql:

First, you will need access to a machine that runs MS-SQL, so if all you have is a .bak file, you will first need to restore a database from it within MS-SQL before you can covert it.

(Skip this step if your DB already up  and running).
If you don’t have MS-SQL running on your machine, download SQL Server Management Studio Express, a free tool from Microsoft that allows you to work with MS-SQL databases.
After installing, open the program and see if you can connect to your local machine. In my case I couldn’t becauseI didn’t have all the required services running.

To start the services, go to “Control Panel -> Administrative Tools -> Services” and look for 2 services: “SQL Server”, and “SQL Server Browser”. Start those. Now SQLSMSE should let you connect.

Restore DatabaseTo restore a database from a .bak file, simply right-click on the Databases item in the Object Explorer, select “Restore Database” and follow the directions. Voila, you should end up with a running MS-SQL database.

Now for the actual conversion to MySql:

You will need to have a MySql ODBC connector installed on your machine, which you can get from http://dev.mysql.com/downloads/connector/odbc/5.1.htm
(You don’t have to register to get the driver!)

Once installed, you need to add a new data source to your machine. Go to “Control Panel -> Administrative Tools -> Data Source” and under User DSN click “Add” and select the newly installed MySql ODBC driver. Then point this to the target MySql database (if it doesn’t yet exist, create it first).

Great! So now we have a source database, a target database, and both can transfer data via ODBC. All that’s left to do is fire up the Import / Export Data Wizard that comes with MS-SQL and select the appropriate input/output settings, and convert.

Filed under: Rnadom Sftuf — by Richtermeister

Problems Hosting Symfony 1.2 (!) sites on Godaddy?

– 9:58 am

If you’re trying to deploy a Symfony 1.2 site on Godaddy, you may run into some issues that are not remedied by my previous post (which was limited to Symfony 1.0).

The main error you might get looks something like:
Empty module and/or action after parsing the URL “/frontend_dev.php” (/).

To remedy this, you need to edit the factories.yml and add a parameter to the request params, like so:

      path_info_key: REQUEST_URI

(Do not confuse this with the “routing” settings that are already in the file. The “request” settings are commented out in the bottom, so you may not see them right away, and the code above just changing the default path_info_key from PATH_INFO to REQUEST_URI. Just add it below the “routing” settings, on the same indentation level.)

After this is done things may still not work. What I found is that Godaddy adds the script filename to the beginning of the $_SERVER["REQUEST_URI"] variable. This is different from most other hosts I’ve encountered, and it confuses symfony. And me.

So here’s a hack around that. In the /config/ProjectConfiguration.class.php add the following command to the top of the file:


All this does is remove the script name from the REQUEST_URI before symfony bootstraps. Simple but effective.

That should do the trick,
happy shared hosting!

Hang from Flying Bug blog just posted some other interesting godaddy tweaks and corrected an indentation error in my original post. Much appreciated - go check out his blog as well: http://xiehang.com/blog/2009/07/03/symfony-and-godaddy/

Filed under: Symfony — Tags: , — by Richtermeister

IE 7 rejects my session for domains with “_”

December 9, 2008 – 4:38 pm

For this problem I want to bill Microsoft for an hour!
Here’s the gist: watch out when you use “_” in sub-domains.

Today I copied a website to a different subdomain on our dev-server, and I thought I add a “_bak” to the domain to tell things apart. All worked well, I thought, until my coworker tried to test the site using Internet Explorer 7 and found that she was unable to log in. No error either, just the same blank login form after submitting valid credentials.

Since all was working fine in Firefox, my first hunch smelled of cookies ;). And yup, IE rejected domain cookies from this sub-domain, and consequently session_start() generated a new session id for every request, effectively clearing the $_SESSION array which I was using for authentication.

Great Microsoft. Thanks for watching out for me. ,,|,,

Filed under: Rnadom Sftuf — by Richtermeister

static variables and caching

August 24, 2008 – 1:11 pm

If you’re working with classes a lot, you’ve probably come across the “static” keyword for properties and methods, enabling you to call these members straight on the class itself, without instantiating an object.

class UserPeer
protected static $userlist;

public static function getUserList()
return self::$userlist;
What’s a little less frequently encountered is a static variable inside a function (or a method for that matter). Say you have a function that returns something rather computing-intensive, but that’s called multiple times from different areas and you don’t want to re-compute every time, and you also don’t want to drag a global variable around.. then static is for you. Check this out:

function get_world_formula()
static $result;
$result = //compute complex stuff here;
return $result;

Voila! A self-contained caching function. Obviously $result persists only for the duration of one request, but that’ll do just fine if it cuts down some CPU cycles. (It does add to the memory usage though, so be selective where you use it.) If you want to take this one step further, you can make caching optional by adding an extra parameter.

function get_world_formula($use_cache = true)
static $result;
if(!$result || !$use_cache)
$result = //compute complex stuff here;
return $result;

That’s it. Small but cute.

A word of OOP caution! I would not recommend using this approach in objects-methods, because this one static variable would be the same accross all object instances, so all objects would return the same data the first method call yielded, and this may not be appropriate for other objects. Use object properties instead to store chached results.

Filed under: Rnadom Sftuf — by Richtermeister

Problems Hosting Symfony sites on Godaddy?

July 24, 2008 – 5:11 pm

Please Note: There is an updated version of this post for Symfony 1.2

For all who are having problems getting a Symfony app to play nice with GoDaddy, here’s what you (most likely) need to tweak. First of all, I had to make the following entry in my settings.yml file:

path_info_key:          REDIRECT_URL

Then, and this was the real tricky part, I also needed to place a file called php5.ini at my site root, that contains the following line:

cgi.fix_pathinfo = 1

This tells the godaddy server to actually respect this front.php/somepage syntax. Otherwise this will throw a 404 error.

Hope this helps.

Filed under: Rnadom Sftuf — by Richtermeister
Older Posts »

Powered by WordPress