CakePHP GraphViz Models

I have completely and totally rewritten my old script that generates a graph of CakePHP models and their relationships.  Instead of pasting the code in here, I pushed all of its development to GitHub where it now enjoys a new repository.  Please have a look, try it out, and let me know if it does or doesn’t work for you.

The major changes in this version are:

  • Rewritten as CakePHP Shell instead of being a standalone madness script.
  • Got rid of all dot markup. Utilized Image_GraphViz PEAR package instead.
  • Added support for old and new CakePHP versions (1.2, 1.3, and 2.0).
  • Added option for using only real models (via className property of the relationship) or sticking with the old behavior.
  • Added a bunch of options for tweaking GraphViz output.  And now it’s obvious where to edit them, in case you need more.
  • Improved the styling of the graph a bit – fonts and colors.
  • Improved documentation.

As a side effect improvement, now that it is a native CakePHP Shell, it’s trivial to add to your project build process.

Branches graph at GitHub

One of my favorite features of GitHub (and, probably, pretty much any other git client) is the graphical representation of branches.  It usually gives a crystal clear picture of how the source tree came about to be.  But I think today I actually managed to confuse the heck out of it.  Have a look at the screenshot below.

A couple of commits ago it was a master branch (black) and a stable branch (magenta).  Then I (if I remember correctly) created a test branch from the latest stable, pushed it, switched to stable branch, reverted one commit, and pushed it as well, then added a couple of commits to master and merged it into test.  Pushed and got the thing above.  Weird looking source tree mutant.

Anyway, I’m sure it will sort itself out some time later.

P.S.: Indeed, as expected, after just a few more merges, commits, and pushes the tree sorted itself out.  Lovely GitHub, very lovely!

MySQL export CSV into OUTFILE triggers “access denied” error

I came across a weird problem today.  Gladly, the web is full of solutions, but I’m going to post this anyway, just to have it nearby for the next time.  I needed to export the results of some query into a CSV file directly from MySQL.  I prepared my query, made sure that I can see the correct results and than changed it to export into the file. The query looked something like this:

SELECT id, field1, field2, field3
INTO OUTFILE '/tmp/data.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM data_table
WHERE field1 = 0;

I was quite surprised to find myself staring at:

ERROR 1045 (28000): Access denied for user ‘db_user’@’localhost’ (using password: YES)

My database user definitely had full access to the database.  I definitely could see the results of the query before the redirect to the file.  And I definitely had enough permissions to create files in /tmp directory.  And on top of that, I’m sure I used MySQL export functionality a gadzillion times and it always worked without any problems.   What’s wrong this time?

A quick search around got me to this Stack Overflow question.  Apparently, database user has to be given a FILE privilege, which is global (not per-database).   Here is what I did to solve the problem (you’ll need to use MySQL root user of course):

USE mysql;
UPDATE user SET File_priv = 'Y' WHERE User = 'db_user';
FLUSH PRIVILEGES;

I think that it worked for me before was because I exported as root, who does have this permission set to ‘Y’.

Disable and enable CakePHP plugins on the fly

I am currently working on a rather large project which is based on CakePHP framework.  In order to simplify the task, I’ve broken down the whole application into a number of CakePHP plugins.  Now, however, I want to enable/disable plugins on the fly.  After a brief search around I couldn’t find how to do that.  Asking a question at #cakephp IRC channel did it.  RabidFire instantly replied with the link that gave me an idea.   30 seconds later I had a working solution.

CakePHP plugins extend AppController.  So all that one needs to do is add the following lines to app/app_controller.php (Using CakePHP 2.0, but it’s trivial to adopt for earlier versions):

public function beforeFilter() {
    $allowedPlugins = array('crm', 'articles');
    if (!empty($this->request->params['plugin']) && !in_array($this->request->params['plugin'], $allowedPlugins)) {
        throw new ForbiddenException();
    }
}

Command line PHP

I’ve discovered two things about command line PHP today.  I’ll share them here just in case you missed them too.

First, the “-f” parameter is optional.  I’m not sure when the change happened, but I’m pretty sure back when I started using it, it was required.  Now, instead of “php -f some.php” you can run just “php some.php”.

Second, there is now an interactive PHP mode! This is something I wanted for years.  Prior to PHP I had experience with Perl and Python, both of which have interactive modes.  With PHP I had to resort to a gadzillion of tiny files with snippets of code.  Now I don’t have to anymore.  Just run “php -a” and type away.

Have a look at PHP command line options manual page.  Maybe you’ll discover something else.