Showing posts with label carriers. Show all posts
Showing posts with label carriers. Show all posts

Wednesday, November 3, 2010

Controller: Carriers

Controllers provide the functionality of your application. Controllers are named plural and "controller" is explicitly listed in the name.

Create /app/controllers/carriers_controller.php
<?php
class CarriersController extends AppController {
    var $name = 'Carriers';
    var $scaffold;
}
?>
If the controller is using a model other than, or in addition to, the one with the same name as its name, you can access that model in one of two ways.
  1. Add var $uses = array('Carrier', 'Other');
  2. Access the other model directly in code via $this->Carrier->Other...
Adding the Carrier model in the Carriers controller in option 1 is unnecessary since it is automatic, but is listed only for clarity.

The variable $scaffold turns on automated scaffolding for testing and viewing of database data. After testing is complete, ensure to remove the scaffolding line to prevent unauthorized access. If you test with scaffolding, any defined methods will override the scaffolding.

Upload the file and browse to http://drug-ed.com/carriers to verify it works, then add a few carriers.
NameExtension
Verizon PCSvtext.com
AT&T PCStxt.att.net


Observe that since there is a defined relationship between Carriers and Users the actions "New User" and "List Users" are available on the Carriers page. Since there is no Users controller the links don't work yet.

Update Model: Carrier

As mentioned previously, to coordinate with the user model, add the relationship to the carrier model. Edit /app/models/carrier.php and add
    var $hasMany = array('User');
Upload the file.

Model: Carrier

Create /app/models/carrier.php
<?php
class Carrier extends AppModel {
    var $name = 'Carrier';
}
?>
Upload the file.

Database: Setup Carriers Table

Create carriers table
CREATE TABLE IF NOT EXISTS `carriers` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(40) NOT NULL,
  `extension` varchar(30) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;