How to fix OS X WiFi not reconnecting after sleep

I’ve had this problem for weeks: my MacBook Pro won’t automatically reconnect to the WiFi network after sleep.

I did search on the web, but the solutions other provided did not work for me.

This is the solution that did work for me (and others too I hope):

  • Open System Preferences > Network
  • Select the WiFi interface
  • Click the Advanced button
  • Remove your WiFi network
  • Apply and close
  • Connect to your WiFi

And, optional I think:

  • Reopen the Advanced panel
  • Drag your network to the top of the list

Now your Mac should automatically reconnect to the WiFi!

CakePHP pagination sort by grouped and custom fields

Cake’s Pagination component gives us – almost – everything we need to paginate some Model data: query management, sort functions, number links, etc, everything perfectly tied together.

One thing that I needed and it did miss was the possibility to sort by a custom field (generated via SQL SELECT … AS …). It only allows, at first glance, to sort by table columns and virtual fields.

Take this code as a very basic example:

$this->paginate = array(
 'fields' => array('', 'COUNT( as posts_count'),
 'joins' => array(array('table' => 'posts', 'alias' => 'Post', 'type' => 'LEFT', 'conditions' => array(' = Post.user_id')))
 $users = $this->paginate('User');

If you wanted to sort by posts_count you have to do a little hack: set posts_count as a virtual field, pointing to itself. Put the following code before the call to paginate():

$this->User->virtualFields = array(
 'posts_count' => 'posts_count',

That’s all!

WordPress “Error establishing a database connection” when using W3 Total Cache

This problem should be pretty rare, but I’m going to share in case someone else is having the same problem:

  • the homepage of your wordpress sites returns “Error establishing a database connection”
  • it only happens when not logged in (try visiting /wp-login.php)
  • you are using W3 Total Cache

In my case, the root cause of the problem was a server migration without copying files I thought were not needed (caches and temp files). I rsynced the whole site excluding “tmp” and “cache” paths, since those are usually regenerated on need. You may experience this problem because of other reasons, but the fix is pretty simple: login to wp-admin and clear all W3 Total Cache caches!

Download session protected websites using wget and Google Chrome

Downloading http auth protected websites using wget is pretty easy, you just add –user and –password parameters to the command:

wget --user=foo --password=bar

But most of the websites do protect content using a more user friendly html login page, which does not use http authentication, but server side code to check the user credentials against a database or auth server. Continue…

Keep body background image aligned with content

This is probably not a very common issue, but here’s a nice trick to keep the background image of the body element aligned with the page content (instead of window border). You may want to obtain this effect, for example, when using a particular background image which you prefer to show near the border of your main wrapper. Being based on Javascript you should not use this method to provide core functionality to a web page. Continue…

Enable display of PHP Errors via htaccess

Nothing is more frustrating than a PHP’s blank-page-of-death. You don’t know what’s happening and nothing can help you more than the output of a error message!

Here is how to enable output of PHP errors on the page using .htaccess on Apache. Simply create a file called .htaccess in your website directory (if it doesn’t exists) with the following content:

php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on

Expanding CakePHP multistep forms with dynamic fields and serialized data

As a sequel to the post about multistep forms with CakePHP, I’d like to add a little bit of complexity to that sample. This time we’ll add some dynamically generated fields and, in general, serialized data.

Dynamically generated fields can be used, for example, when the name and type of the fields is not known in advance, and therefore cannot be hardcoded in the script. Another situation where serialized data is a good solution is when you have a form with lots of fields for whose you don’t want/need to create the relative database field. Continue…

Write less code: merge Save and Edit in CakePHP

In most cases writing less code gives the developer a series of advantages: it’s easier to maintain and extend, easier to read and easier to debug! In this post I’m going to suggest a little CakePHP tip that in the long run will save you a lot of coding.

Some develepers already implement this method, but I think it deserves much wider usage: I’m talking about merging model save and edit in a single controller method. Continue…