Archive for the ‘CakePHP’ Category

Baking with XAMPP on Windows

Monday, September 14th, 2009

I’m not sure if it’s just my set-up but when I run cake.bat and say, try to bake a model I get

  1.  
  2. Fatal error: Call to undefined function mysqli_connect() in C:\…\cake\libs\model\datasources\dbo\dbo_mysqli.php on line 73
  3.  

The reason is that php.exe when run from a command line uses a different php.ini than PHP that’s run as an Apache module. To fix this edit cake.bat and add -c"C:\xampp\apache\bin\php.ini". This is the path where XAMPP placed php.ini.

Getting rid of JOINs in updateAll() query

Sunday, September 13th, 2009

I guess I haven’t mastered the model relationships enough in order to appreciate them - and use correctly. On one hand they seem to be really cool and provide an easy access to linked database records while on the other hand, they just seem to stand in the way too often.
(more…)

Switching layout using a url parameter

Sunday, February 8th, 2009

I needed to open a static page in a pop-up window. For static pages we have a handy controller Pages but I wanted to use a different layout for each page and do it in a flexible way.
(more…)

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’)