Archive for June, 2008

 

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.

 

Been out the entire day. I wanted to walk around the city alone a little longer actually. Thought I look around a little more, but my mind went wondering to begin a reflection process. Feeling lost, I head home.

I think I have changed a lot since the polytechnic days and it’s not really the way I want it.

I keep thinking “if I were to do things differently, I would have so and so…” but everything’s stuck the way it is. Past cannot be altered.

My personality seemed malleable, changing from occasion to occasion, different periods of the year I’m in different ‘personalities’.

I am starting to feel I lack a distinct personality and that I just sort of “went with the flow” because things appear to be working (so far).

But what if I am wrong?

[Ignore this post...]

 

Sighted this an one of the stores, it’s tips that would eventually go for donation:

So many one cent coins in donation

So many one cent coins! I didn’t even know they’re still around. I thought the mint stopped producing these already and no stores seem to be offering them as change.

 

I was reading through a company’s terms of service when I come across this:

LIMITATION OF LIABILITY

Under no circumstances shall the company be held liable for any delay or failure in performance resulting directly or indirectly from acts of nature, forces, or causes beyond its reasonable control, including without limitation, internet failures, computer equipment failures, telecommunication equipment failures, other equipment failures, electrical power failures, strikes, labor disputes, riots, insurrections, civil disturbances, shortages of labor or materials, fires, floods, storms, explosions, acts of God, war, governmental actions, non-performance of third parties, or loss of or fluctuations
in environmental factors.

Yes, it’s an internet company. Won’t tell you what company it is, hahaa… But it’s not hard to guess.

God is a righteous judge, a God who expresses his wrath every day. (Psalms 7:11)

You see, sometimes, servers can be involved indirectly to man’s sin. Through the wrath of God may His people shun the sinful nature of those servers.

 
  • @uzyn: the last time i had no internet, i had anxiety, spasm and insomnia #
  • @miccheng weekends are slow days, haha #
  • just discovered TwitterFox, it’s a great firefox extension, i found Twirl a little too intruding and Twitter web a little too much a hassle #
 

Thirty cents. Better than nothing. I’m literally using the money earned by another blog to pay for the existence of this blog. It’s sad that I spend more effort in this blog but I don’t get the visitors.

Some notes on ad revenue

Thoughtful posts don’t gain you visitors.

To get visitors, you need, I repeat, you need to blog silly things.

No one wants to know your opinion unless you’re a celebrity and have some say in things.

If part of your reason of blogging is to earn some pennies, never start a personal blog.

Start a link blog or something. Put up music for people to download (and violate from ahem ahem stuff), put lots of pictures that are either funny or cute. (Cute pictures like dogs, cats and rabbits are a winner.)

Don’t bother posting those male-female analysis, boy-girl relationship that could have been passed off as dissertation from some academic institution.

Blog with what’s trendy. Bill Gates retire? Blog about it, link it to another blog. iPhone released? Post pictures, and write a bunch of useless iPhone specifications.

Conclusion

Just don’t model your income generator after this blog.

At the end of 4 months, you will just earn thirty cents. That’s just my two cents. Okay, now I’m left with 28 cents, great.

 

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.

 

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