Posts tagged with ‘cakephp’

 

Okay, Django 1.0 alpha has been released finally. I don’t have the time to test this out but I played around with some pre 1.0 alpha builds a couple of weeks back and that time the software was quite stable already.

Django 1.0 alpha released!

In accordance with the Django 1.0 release roadmap, tonight we’ve released the first “alpha” testing version of Django 1.0. This release includes all of the major features due for inclusion in the final Django 1.0, though some lower-priority items are still scheduled to be included before the 1.0 feature freeze, which will occur with the first beta release next month.

The next step on that path will be the first Django 1.0 beta release, currently scheduled for August 5. If you’d like to help out, please review our documentation for contributors and feel free to join in one of the development sprints scheduled for the run up to 1.0; the full schedule is available in the Django 1.0 release roadmap.

Now when would CakePHP 1.2 be released?

 

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:

<?php
class AppController extends Controller {
  var $scaffold;
}
?>

…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.

<?php
// C:\justrealized\trunk\main\
$main_app = ROOT.DS."main".DS;
// C:\justrealized\trunk\main\models\
$main_app_models = $main_app."models".DS;
// Add these to modelPaths
$this->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"));
?>

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:

<?php
class AppModel extends Model {
}
?>

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:

<?php
# db config from C:\justrealized\trunk\main\config\database.php
require(ROOT.DS."app".DS."config".DS."database.php");
?>

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:

<?php
class AppController extends Controller {
  var $scaffold;
}
?>

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

<?php
class ClassroomsController extends AppController {
  var $name = "Classrooms";
}
?>

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:

<?php $models = Configure::read("ModelsToLoad"); ?>
<h2>Listing all available models</h2>
<ul>
  <?php foreach($models as $model): ?>
  <li><?php echo $html->link($model, "/".low($model)."s"); ?></li>
  <?php endforeach ?>
</ul>

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

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.

 

CakePHP 1.2 comes with in built PagesController which helps you easily create static pages. It’s easy to set up, you just have to put your pages view in /views/pages/. All the codes are for routes.php.

<?php
# View is in /views/pages/home.ctp
Router::connect(‘/’, array(‘controller’ => ‘pages’, ‘action’ => ‘display’, ‘home’));
?>

The page title ($pageTitle) would be ‘Home’ for the above.

If you want your URL to be /about/my-company, you can do the following:

<?php
# View is in /views/pages/about/my_company.ctp
Router::connect(
  ‘/about/my-company’,
  array(
    ‘controller’ => ‘pages’,
    ‘action’ => ‘display’,
    ‘about’,
    ‘my_company’,
  )
);
?>

The page title would be ‘My Company’ this time since underscores are converted to spaces. Pretty useful.

 

A month back, I talked to a friend I shall call Joel since his name is really Joel anyway. We had a short geek talk and he asked me if I have any knowledge on open-source projects.

“I only know how to use them, I don’t code for them,” I answered.

He continues, “but do you know of any open source project that is worth my time?”

“Well, it sort of depends on what you like…”

He interrupts, “It doesn’t matter, I want to join a project that is popular.”

“Huh?”

And he elaborates to me, “If you want to join an open source project, should join one that is popular then can gain recognition.”

That was a sad moment for me. I shrugged and suggested “erm, maybe can try Firefox.”

On a side note, Uzyn is excited that CakePHP 1.2 Release Candidate 1 is out. I’ve been longing to consume the release candidate. Great work CakePHP team! I love your work.

Fruity birthday cake with kiwi mango and strawberry

Ahh, just some random picture of a cake I took.

 

CakePHP introduces a new way of writing your routes, you can now restrict your URLs at parameter level, meaning that we can now specify URLS like /date/:year/:month instead of /date/*. This post compares CakePHP 1.1 and 1.2 routing differences. I’ll start with showing the old way first:

CakePHP 1.1 way

In CakePHP 1.1, routings looks like this in /app/config/routes.php:

Router->connect(‘/date/*’, array(‘controller’ => ‘posts’, ‘action’ => ‘p_date’));

And the corresponding method in the controller would look something like this:

function p_date($year, $month) {
  $posts = $this->Post->findAll(
    "MONTH(pub_date)=$month AND YEAR(pub_date)=$year"
  );
  $this->set(‘posts’, $posts);
}

CakePHP 1.2 way

There isn’t much changes in CakePHP 1.2’s routing handling statement except that now it uses 2 colons.

Router::connect(‘/date/*’, array(‘controller’ => ‘posts’, ‘action’ => ‘p_date’));

No change in the controller’s method. (more…)

 

One of the easier ways to retrieve data from MySQL by month and year is to use one of MySQL’s built-in functions - MONTH() and YEAR(). The following is a piece of CakePHP code.

function p_month($year, $month) {
  $posts = $this->paginate(
    ‘Post’,
    "status=’approved’ AND MONTH(pub_date)=$month AND YEAR(pub_date)=$year"
  );
  $this->set(compact(‘posts’));
}

If you don’t use CakePHP, a SELECT statement in MySQL would be:

SELECT * FROM `Post` WHERE `Post`.`status`=‘approved’ AND MONTH(`Post`.`pub_date`)=2 AND YEAR(`Post`.`pub_date`)=2008;

The above extracts February 2008 data. Pretty neat.

Previous I wrote crap like:

SELECT * FROM `Post` WHERE `Post`.`status`=‘approved’ AND `Post`.`pub_date`>=‘2008-02-01′ AND `Post`.`pub_date`<‘2008-03-01′;

It’s just messy and ugly. And lots of calculation have to be done before hand.

Hope it helps. I haven’t really tested the SQL statements by the way. But it should work correctly. Only tested the CakePHP code. I use CakePHP 1.2’s paginate function.

 

I meant this as a what-did-i-do-today post but it seems like by the time I actually gather myself to type this things… Well, it’s passed 12 already. So this is a what-i-did-yesterday post.

Coding with CakePHP (PHP)

My work involves writing PHP codes. I don’t really like the syntax now that I see Python. I still don’t like the braces all over. But well… I kinda have more confidence in that. I did some work with Ruby and honestly, I think I really should stick with PHP.

coding-in-cakephp

Coding with Django (Python)

Oh I did a bit of learning today. Learning Django has been fun. I looked at their documentation, the developers are just amazing. So much thought has been given to the DRY principle and Django sort of enforces you to do that to a certain extend to.

My next project will be in Python/Django. It’s more of a personal project actually. But it would be interesting to see how it can work with my current company’s product. Erm… Yeah, after the examinations.

 

WordPress powered and Django inspired.
Love and elephants come after.
RSS: Posts and comments.