Archive for the ‘CakePHP’ Category

Populating a SELECT box with distinct values

Wednesday, December 10th, 2008

Let’s say you log events into a database table Log. Attribute event holds event type and you want to filter the log listing just by that.

Here’s what you probably try at first:

  1.  
  2. $this->set(‘events’,
  3.   $this->Log->find(‘list’,
  4.     array(‘fields’ => ‘DISTINCT Log.event’, ‘Log.event’)));
  5.  

It’s not gonna work and you will have to use find(‘all’) and Set::combine() to get what you want:

  1.  
  2. $events = $this->Log->find(‘all’,
  3.   array(‘fields’ => ‘DISTINCT Log.event’));
  4. $this->set(‘events’,
  5.   Set::combine($events, ‘{n}.Log.event’, ‘{n}.Log.event’));
  6.  

SQL functions or keywords in CakePHP conditions

Tuesday, December 9th, 2008

For those who missed the information, starting from CakePHP 1.2 it is no longer possible to use the magic tag -! in your condition to prevent your SQL function or keyword to be enclosed in quotes.

The recommended way now is to place it into the key or not make the array associative at all. It is no longer possible to put it into the value part of the definition.

So instead of:

  1.   ‘DATE_ADD(Payment.modified, INTERVAL 2 DAY) >’ =>  
  2.   ‘CURRENT_DATE’)

you have to use:

  1.   ‘DATE_ADD(Payment.modified, INTERVAL 2 DAY) > CURRENT_DATE’)

What you can expect as $results in afterFind()

Sunday, November 16th, 2008

Function afterFind() can be called in various contexts with different types of $results. If you don’t handle them all properly the function will not work right (which you may not care in some cases) and you’ll end up with notice level errors.

(more…)

Ganymede / PHPEclipse – not a good choice for debugging CakePHP

Friday, November 14th, 2008

After hours of trying I finally conclude that CakePHP cannot be debugged in Eclipse 3.4.1 (Ganymede) and PHPEclipse 1.2.1.200810130444PRD (XDebug).

My webroot is set to app/webroot and Eclipse stops on a breakpoint only if it’s in app/webroot/index.php
Even after it stops there it won’t stop on a next breakpoint that is set in my controller.

The only working solution I found is downgrading to Eclipse 3.3.2 (Europa) and PDT 1.0.3.v20080603. Fortunately both can run side by side so in theory, I can write new code in Ganymede and debug in Europa but that sucks. Programming is a constant debugging.

I could try to switch from XDebug to DBG. Maybe I will next time I have a day that I can waste.

Update:

New PDT 2.0.0.v20081030-1802 works with Ganymede/XDebug just fine. It also has features I have been missing like Mark Occurrences – one more reason for switching. It doesn’t have support for chm help files though (PHPEclipse does) and is generally slower than PHPEclipse.