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.

Tuesday, October 20, 2009

Securing the application

Lets say we have two different applications which are part of bigger system so they have the same set of users. If it is some closed system you will probably want to restrict some groups of users from accessing one o the applications (ie. client didn't buy that feature so his users shouldn't use it). If you wanted to use the standard credentials system implemented in the symfony, you'd have to check in each action if user have a proper credential.  In our system we have hundreds of actions so it'd be a real nightmare. Especially you'd have to do it for both apps.

Wednesday, October 14, 2009

Double submit issue

I know I know, it had been discussed to death before. There are some solutions but in our application the new problem arised. The main problem are not impatient users cos our forms are secured against double-clicks. The thing is: we're using ExtJS and thus our application awaits for callbacks to display appropiate message to the user. But as some our users use mobile internet the connection faults happen quite often. When callback don't reach the browser, the script displays connection error message.

Monday, October 12, 2009

First post ;)

Took me some time to decide if I should start a blog or not. But in the end I thought it is a good idea to store my thoughts and solutions at some place where I can come back to. So here I am ;) I choose blogspot cos I don't really have time for writing my own blog. Besides, why should I reinvent the wheel? :P

In this blog I'll focus on Symfony framework and overall PHP issues I happen to step by during my work.