Tuesday, November 10, 2009

The Law of Demeter

Today I've read a very interesting blog post. What brought my attention is thing which bothered me for quite a long time in the past, I mean this:

Most of the Symfony projects I’ve worked on are rife with lines of code like this (in the actions):

$this->getUser()->getGuardUser()->getProfile()->getEmail()

To conform to the above ideals (The Law Of Demeter) the code should be re-written as:

$this->getUser()->getEmail()

My solution for that problem is using magic method __call() in myUser class:

public function __call($method, $arguments)
 { 
  if(is_array($arguments))
  {
   return call_user_func_array(array($this->getGuardUser()->getProfile(), $method), $arguments);
  }
  else
  {
   return $this->getGuardUser()->getProfile()->$method();
  }
 }

It's possible because both tables have 1:1 relationship. This way we basically merge both objects and don't have to create dozens of "shortcut" methods.

Saturday, November 7, 2009

Sympal installation fix

Recently I was trying to install the Sympal plugin for Symfony. Everything was fine until I tried to run the application. Symfony has displayed an 404 error unfortunately telling me nothing useful:

Unable to find the 
Component : Content
Table : content
object with the following parameters "array ()").

I've cleaned the project up and step by step installed the plugin again. And... I saw the same error. So I thought "wtf?" and started googling for help. I thought: it's quite popular plugin, I should find tons of posts and manuals about it. I was wrong, I found only few posts about my problem and no solution. I didn't give up and in the end on some mailing list someone found the reason and was kind enough to share it.

It looks like there's a bug in fixtures: in content and menu_item tables all records have the date_published column set to NULL while the is_published flag is set to TRUE. So to fix the error set date_published=NOW() and run the app. Now everything should work as expected.