Showing posts with label reminders. Show all posts
Showing posts with label reminders. Show all posts

Wednesday, November 3, 2010

Controller: Reminders

Create /app/controllers/reminders_controller.php
<?php
class RemindersController extends AppController {
    var $name = 'Reminders';
    var $scaffold;
}
?>
Upload the file and browse to http://drug-ed.com/reminders to verify it works. Again the defined relationship to drugs and users should cause action links to show.

Model: Reminder

Create /app/models/reminder.php
<?php
class Reminder extends AppModel {
    var $name = 'Reminder';
}
?>
Each reminder "belongs to" a user, and each user can "have many" reminders, so there is a one to many relationship between the two tables. To indicate this to Cake, add the following information below the $name variable:
    var $belongsTo = array('User');
In addition, each reminder has a one to many relationship with drugs, so it "belongs to" a drug. Add 'Drug' after 'User' in the above line.

When designing your database, ensure that each model that "belongs to" another model has the appropriate foreign key field. For example since each reminder belongs to a user, that user is identified by the key "user_id" in the reminder table. If you stray from convention (classname_id) you have to define the foreign key explicitly in your model.
    var $belongsTo => array(
        'User' => array(
            'foreignKey' => 'UniqueUserID'
        )
    );
Upload the file.

Database: Setup Reminders Table

Create reminders table
CREATE TABLE IF NOT EXISTS `reminders` (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL,
  `drug_id` int(11) NOT NULL,
  `days_of_week` varchar(7) NOT NULL,
  `time` time NOT NULL,
  `created` datetime NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;