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.

Feeling lost while returning home

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

Rather strange Terms & Conditions

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.

This blog finally made money

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’s built-in Pages Controller

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.

[code lang=”php”] ‘pages’, ‘action’ => ‘display’, ‘home’));
?>[/code]

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:

[code lang=”php”] ‘pages’,
‘action’ => ‘display’,
‘about’,
‘my_company’,
)
);
?>[/code]

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

AVG LinkScanner pretends to be IE6, screws up analytics

Speaking of Internet Explorer, AVG has been disguising as Internet Explorer to visit websites. Web developers and webmasters aren’t too pleased. AVG’s LinkScanner is estimated having to be downloaded by more than 20 million people. The LinkScanner attempts to disguise itself as a real live human click claiming itself to be Internet Explorer 6. It just screws up web analytics.

AVG disguises fake traffic as IE6

…webmasters who rely on log files for their traffic numbers may be unaware their stats are skewed. And others complain that LinkScanner has added extra dollars to their bandwidth bill.

…(Paid AVG) appears that scans now use these agents as well:

  • Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
  • User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
  • User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;1813)

…the first agent is by far the most common. Which is bad news for webmasters. That’s also the Internet Explorer 6 user agent. Unlike the other two – and the original “1813” agent – it’s a perfectly valid agent that may turn up with real clicks.

AVG’s chief of research Roger Thompson says the for-pay LinkScanner is only using the IE6 user agent

In an effort to fix this problem, one web master advocates redirecting AVG scans back to AVG’s site. “Many webmasters simply tell LinkScanner to scan AVG’s site instead, so their site gets marked as malware free every time – while AVG gets handed the extra bandwidth cost,” says the webmaster of TheSilhouettes.org.

But this assumes that AVG is using a unique agent. And at the moment, it’s not. The send-it-back-to-AVG method may redirect legitimate clicks as well.

Which gets to the heart of the matter: AVG’s security philosophy is fundamentally at odds with webmaster peace of mind. The company wants to scan search results, and it wants to scan them in a way that’s difficult to distinguish from real traffic. “In order to detect the really tricky – and by association, the most important – malicious content, we need to look just like a browser driven by a human being,” AVG chief of research Roger Thompson has told us.

And if that causes problems for webmasters, Thompson says, so be it. “I don’t want to sound flip about this, but if you want to make omelets, you have to break some eggs.”

Clearly, the company doesn’t fully realize the importance of web analytics.

“In order to make an omelet you have to crack some eggs. But a good omelet has cheese, ham, peppers, mushrooms and all sorts of other ingredients which AVG seem to have forgotten about.”

But AVG continues to say it’s working to solve the problem – including the bandwidth issue. Referring to LinkScanner’s new IE6-like user agent, Thompson told us, “We intend to leave those in place until we can find the right balance point which will allow us to continue to provide the best possible protection for our customers, without imposing too much extra bandwidth on websites.” (Source: The Register)

[ad#highlight]

I was reading what the chief of research had to say – “If you want to make omelets, you have to break some eggs.” Sir, that’s just a nonsensical comparison, when you break eggs, you don’t get your neighbors to pay for them.

Bandwidth is a clear issue. It is not free. This solution is another example of how innocent parties are penalizes just because of a tiny number of visited sites infected with malware.

(By the way, I go around disguised at GoogleBot as my user agent. But that won’t hurt your bandwidth, I have reasons for doing so.)

Don’t eat us, please…

Love us, not eat us. All lives are precious. Well something like that it says here:

Please protect us and not eat us

Chinese loves pigs by the way. When there’s a lack of pork or the prices are not right in China, there may be revolts. The Chinese government stock up pork for emergency. Singaporeans, on the other hand, loves chicken. We have chicken burger, chicken rice, chicken drumstick and chicken pie. It’s probably an integral part of the local staple (I typed stapler) diet.

I like dogs so I don’t eat them.