CakePHP 1.2 and PHP 5.3 doesn’t play nice

I just upgraded all the software only to discover PHP got upgraded to 5.3. That’s fast, PHP 5.3 isn’t out too long ago and it’s in the Macosforge repository. Even the CakePHP 1.2.4.8284 update released on August 9 did not include PHP 5.3 compatibility.

Perhaps it’s time to update the CakePHP requirements and be indicative of the lack of support for PHP 5.3.

Now the library is throwing deprecated notices all around “Assigning the return value of new by reference is deprecated in…”. Also there is a PHP warning:

[code lang=”php”]PHP Warning: strtotime(): It is not safe to rely on the system’s timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Asia/Singapore’ for ‘SGT/8.0/no DST’ instead in…[/code]

To stop the deprecated notices, I had to modify Cake’s core, not something I like to do.

I added the line “error_reporting (E_ALL & ~E_DEPRECATED);” at the top of /cake/libs/object.php. I also updated /cake/libs/configure.php by changing “error_reporting(E_ALL);” to “error_reporting (E_ALL & ~E_DEPRECATED);”.

[ad#highlight]

This doesn’t solve the problem exactly, it just suppresses the deprecation notices so I can continue working. For a PHP 5.3 compatible CakePHP, you’ll need to use CakePHP 1.3 which currently isn’t stable. I’ll definitely be trying it soon but in the meantime I’ll just have to suppress error reporting.

Update: jdbartlett commented on a better way:

[code lang=”php”]if (defined(“E_DEPRECATED”)) error_reporting(E_ALL ^ E_DEPRECATED);[/code]

If youโ€™re not doing any debugging, you can add the above code to the top of /app/webroot/index.php and suppress the error messages without editing the core. ๐Ÿ™‚

Thanks jdbartlett! You saved me time. ๐Ÿ™‚

How to load models from another CakePHP application

CakePHP is great for administering database relations (tables), something like using PhpMyAdmin. But PhpMyAdmin lacks the ability to associate relations together. Admin interfaces to manage relations through associations are therefore better done using CakePHP 1.2.

However, to create an administrative application on top of your main CakePHP application is just messy and hard to maintain. One of CakePHP’s most powerful feature is scaffolding where you could declare:

[code lang=”php”][/code]

…and all controllers would have scaffolding. Unfortunately, we can’t do such magic on our deployed application. So that leaves us with only one way – make a CakePHP 1.2 Admin application that loads models from our Main application.

In summary, we did this:

  1. Create a new application called ‘admin’ to load the database and models from our existing application ‘main’.
  2. Load the models and do scaffolding.
  3. Create a simple page to link to all our scaffold views.

Ultimately, we want to maintain the admin interface as little as possible. The main application should not have changes in code.

0. Structure for this example

My models are Student, Classroom and Teacher since I like school. Here’s my rough folder structure:

/trunk/admin

  • /config
  • /controllers
  • /views
  • /webroot
  • app_controller.php
  • index.php

/trunk/main

  • /config
  • /controllers
  • /models
    – classroom.php
    – student.php
    – teacher.php
  • /views
  • /webroot
  • app_controller.php
  • index.php

1. Bootstrapping in Admin

Edit the Admin ‘bootstrap.php’ in /trunk/admin/config/ to load models from Main application. I’m using Windows Vista, that’s why the C drive.

[code lang=”php”]modelPaths = array($main_app, $main_app_models);

// Models to be loaded, we will use this again
Configure::write(“ModelsToLoad”, array(
“Classroom”,
“Student”,
“Teacher”,
));
// import the models
App::import(“Model”, Configure::read(“ModelsToLoad”));
?>[/code]

Previously in CakePHP 1.1, there is loadModel but that has been deprecated in favor of App::Import(‘Model’, $models);.

Also, make sure you have ‘app_model.php’ in your /trunk/main/, i.e. your Main application:

[code lang=”php”][/code]

2. Getting our database configuration

We wouldn’t want to maintain another database.php, so the Admin will use Main application’s database settings. Editing Admin’s database.php:

[code lang=”php”][/code]

Now we’ll have our models nicely loaded.

3. Setting up Admin’s controllers

We can create a file called ‘app_controller.php’ and place it on /trunk/admin/. We scaffold every controller:

[code lang=”php”][/code]

And we create our three controllers – classroom_controller.php, student_controller.php and teacher_controller.php. It looks like this inside classroom_controller.php:

[code lang=”php”][/code]

4. The Admin is done

Now, go to your Admin application to see your classrooms. Mine is at http://localhost:7171/classrooms/

If everything is done correctly, you should see your admin interface with the default CakePHP theme.

5. Setup the Admin homepage

There’s nothing at the homepage, let’s put links to our views. Remember that we write the configuration ‘ModelsToLoad’ earlier on? Now it’s time to use it.

Create a file called ‘home.ctp’ in /trunk/admin/views/pages/home.ctp. And throw in the following codes:

[code lang=”php”]

Listing all available models

  • link($model, “/”.low($model).”s”); ?>

[/code]

The above reads from the array of models and make links for easy access from the homepage.

[ad#highlight]

Conclusion

Although this looks complicated, but it’s quite easy to set up. When you have a new model, add it to ‘ModelsToLoad’ and create the corresponding controller for it. This post is originally titled “How to create a CakePHP admin inteface by loading models from an existing CakePHP application” but that is just too long. I couldn’t locate a guide on using App::import() and how models can be loaded from external applications so I had to look at Cake’s code. Due to limitations of my WordPress plugins, I had to use double quotes instead of single quotes in all my PHP codes.

Once again, CakePHP’s scaffolding is a wonderful feature. Take some time to play with it, the rewards are tremendous.

How to use TortoiseSVN to get latest Django trunk

If you just want to download the latest Django trunk package, you can just scroll down to the bottom of this page and click on Download in other formats: Zip Archive.

I use an SVN tool called TortoiseSVN to commit projects. The same tool can be used to check out the Django source code. By ‘check out’, what you are doing to retrieving a copy of the Django source code from the online repository.

As Django hasn’t reach 1.0, new features are being rolled out very quickly. These new features deprecate some older ones and adds new things to your to-play list. It’s always good to have the latest version. You can grab it through the SVN trunk. Continue reading “How to use TortoiseSVN to get latest Django trunk”