Using Zend with Symfony2

I recently needed to use the Zend GData component in my Symfony2 project. It is very easy to setup, but there is one mistake that I suspect some other people might make as well.

First, add the Zend library to your vendor folder (e.g. vendor/Zend/library/Zend). As Zend does not use namespaces, you need to register the ‘Zend’ prefix instead. This can be done in the autoload.php file located in your app folder. Apparently Zend has some problems this way with ‘require_once’ statements, hence the modification of the include path (last line) :

// autoload.php

use Symfony\Component\ClassLoader\UniversalClassLoader;

// ... existing code

$loader->registerPrefixes(array(
    'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
    'Twig_'            => __DIR__.'/../vendor/twig/lib',
    'Swift_'           => __DIR__.'/../vendor/swiftmailer/lib/classes',
    'Zend_'           => __DIR__.'/../vendor/Zend/library',
));

$loader->register();
$loader->registerPrefixFallback(array(
    __DIR__.'/../vendor/symfony/src/Symfony/Component/Locale/Resources/stubs',
));
set_include_path(__DIR__.'/../vendor/Zend/library'.PATH_SEPARATOR.get_include_path());

Now you are ready to use the Zend components. One mistake I made, I used the classes without a leading backslash, which does not work because the classes are in de default namespace. So, instead of using e.g.

$gdataCal = new Zend_Gdata_Calendar($client); // class not found error will be thrown

you need to add a backslash as such :

$gdataCal = new \Zend_Gdata_Calendar($client); // class is located in default namespace

Running crontab as root

If you want to run some script as the root user using crontab, make sure you sudo as root before you modify the crontab entries :

$ sudo -i
[sudo] password for xxx :
(enter your password)

$ crontab -e

The sudo -i command will change your current user to root, so crontab -e will automatically use the cron file of the root user.

Symfony2 validation using annotations

The Symfony2 book says that validation using annotations should be done as follows :

// Acme/BlogBundle/Author.php
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
    /**
     * @Assert\NotBlank()
     */
    public $name;
}

I tried this on my installation, but this doesn’t work. I had to change ‘@Assert\’ into ‘@assert:’ to make it work :

// Acme/BlogBundle/Author.php

class Author
{
    /**
     * @assert:NotBlank()
     */
    public $name;
}

Read more »

Symfony2 and required fields

I just came across my first HTML5 ‘issue’. I was testing the Symfony2 forms, and for required fields I always got the message ‘Please fill out this field.’ which was shown in a tooltip. I started looking in the Symfony2 code where this message came from, but I couldn’t find it anywhere. So I started inspecting the generated html, and I saw the ‘required’ attribute on the input fields. Apparently this is a new attribute in HTML5, and most browers already support it. I now realise that HTML5 really is something I should learn more about…

While looking for some more info, I came across an online book that seems very interesting : http://diveintohtml5.org. Definitly on my todo list now :)

Disabling foreign keys in MySQL

Sometimes you want to perform some action on your database which needs foreign keys to be disabled. Luckily this is very easy in MySQL :

SET FOREIGN_KEY_CHECKS=0; # disable all foreign keys

# ... perform your sql magic

SET FOREIGN_KEY_CHECKS=1; # enable all foreign keys

My first experience with Symfony2

After trying the Symfony2 framework for some days, setting up a little project with security, ORM and routing, I have found some things in the manual that were not entirely clear (or sometimes just wrong, I think). Of course it is still in beta, so I’m not blaming anyone but myself… And don’t get me wrong, I will continue using Symfony2 as it is very easy and clear to use.

Translations

Translations don’t seem to work outside bundles. I tried creating a translation file in the app/Resources/translations folder, but Symfony2 doesn’t seem to pick it up. It does work inside bundles, so this is not really a problem for me. Don’t know of it is intended behavior either.
Read more »

Installing the INTL extension on MAMP

I just started developing in Symfony2, and the installation told me I should install the Intl extension (intl.so) for internationalization. Although Symfony2 works without the extension, I suspect I will run into problems sooner or later, e.g. when using the translation service. So after trying some different ways to compile and install the extension, I finally found that it is fairly easy using Macports.
Read more »

Installing Xapian for PHP on Linux (Debian)

Unlike installation of Xapian on OS-X for use with MAMP, the installation on Debian could not be more simple :

sudo apt-get install php5-xapian

Then restart your Apache server

sudo /etc/init.d/apache2 restart

And you’re done :)
Check phpinfo, there should be a section on Xapian now.

PHP, Xapian and… pointers ?

I just discovered some weird behaviour using Xapian in PHP, and I think it has something to do with pointers. I tried adding two XapianDateValueRangeProcessors as follows :

$qp->add_valuerangeprocessor(new XapianDateValueRangeProcessor(0, 'created:', true, false, 1970));
$qp->add_valuerangeprocessor(new XapianDateValueRangeProcessor(1, 'updated:', true, false, 1970));

But somehow this would break Xapian (I didn’t get any error, this script just stopped completely when parsing the query).
Read more »

Searching date ranges in Xapian and PHP

I have not much experience with Xapian, so the following post might seem trivial to some. But it took me some time to figure this out, so I might as well share my findings. After running some basic examples from the Xapian wiki (which worked out of the box), I needed to be able to search for a range of dates (e.g. date created from – to). After reading the very interesting article on the Xapian Search Architecture, I realised I needed to add a value to the document which contained the date field, opposed to the terms I had been adding. So, while indexing, I added following code :

$doc = new XapianDocument();
// some code ommitted such as adding terms, setting data, ...
$doc->add_value(0, '20110101');
// some more code ommitted such as indexing the text - see examples

The first parameter defines the value slot which you want to use. This is just a number, look at it as an index of the value. You will reuse this index when searching, so it’s best to create a constant for each index regarding readability. Other than this, you create and index the document as explained in the examples.
Read more »