Our First DB Controller

From Rhypedigital

Jump to: navigation, search


<?php
 
#
#  File     : Ctrl/Test.php
#  Copyright: 2009
#           : Rhype Digital Ltd
#  License  : This file is released under the terms of the Rhype Digital Software Licence.
#           : Use of this software is prohibited without express permission of Rhype
#           : Digital Ltd.
#  EMail    : copyright@rhypedigital.com
#
 
require_once   ('Lib/View/Test.php'          ) ;
require_once   ('Lib/Model/AR/AR_test.php'   ) ;
require_once   ('RiPHPLib/Support/Class.php' ) ;
require_once   ('RiPHPLib/MVC/View.php'      ) ;
 
#  This is the controller class. By convention the class is named Ctrl_name
#  where name is the controller name (in this case Test).
#
class Ctrl_Test extends RPL_Support_Class
{
   protected   $m_dbh         ;
   protected   $m_cgi         ;
 
   #  Class constructor, typically stores the database and CGI objects for
   #  later use.
   #
   function __construct ($dbh, $cgi)
   {
      $this->m_dbh         = $dbh   ;
      $this->m_cgi         = $cgi   ;
   }
 
   #  There should be a method for each action, with the same name as the
   #  action. Typically, init is the default action.
   #
   function init ()
   {
      #  Create an active record for the test table, and retrieve all
      #  records.
      #
      $arTest  = new AR_test ($this->m_dbh) ;
      $arTest->setMultiple   (true) ;
      $arTest->find () ;
 
      #  Create an instance of the view which will render the page, in this
      #  example passing the test active record. This is then rendered and the
      #  result returned.
      #
      $view    = new Lib_View_Test ($arTest) ;
      return   $view->render () ;
   }
}
 
#  A controller should have a function named name_Factory where name is the
#  controller name (in this case Test). This is called from the RPL_MVC_Controller
#  instance created in the main "index.php" script, and passes through the database
#  and CGI objects. The function creates an instance of the controller class.
#
function Test_factory ($dbh, $cgi)
{
   return   new Ctrl_Test ($dbh, $cgi) ;
}
?>