Models
A Model is a PHP class file which extends the 'BaseModel' class file. The name of the class must be the same as that of the php file i.e if you have a class file 'Main.php' the class name should be 'Main'
All model files must be placed under the '/models/' directory and as mentioned above must include and extend the 'BaseModel.php' class file which is placed in the same directory.
A model can be executed from a controller function using:
$testModel = $this->loadModel('MyModel');
This creates and returns an instance of the 'MyModel' Class. And then just like any other
Object you can call its functions and attributes
The 'BaseController' Class exposes the following functions to work with models:
- protected function loadModel($modelName)
Example:
//This is a controller function
public function index()
{
//Set the protocol ( You can have different pages run on different protocols )
$this->setProtocol('http');
/*
Set the language, this is used by the loadMainContent function.
Values can be whatever you call the folder /views/content/[Language]/
( I recommend using standard Language Code's
for example for canadian english use en-CA )
*/
$this->setLanguage('en-CA');
//Set the template to be used ( Different pages can have different templates )
$this->setTemplate('template1');
//Loads the content file: /views/contents/en-CA/home.php
$templateVars['rightContent'] = $this->loadMainContent('home');
//This loads the Model 'myModel' and Executes the function 'myModelFunction'
$testModel = $this->loadModel('myModel');
$result = $testModel->myModelFunction();
//Sets the return value of the model function to a template variable
$templateVars['results'] = $result;
//Finally, parse the template template1 using $templateVars
$this->display($templateVars);
}
//This is a model function
public function myModelFunction()
{
$return = '';
/*
Below are all functions that are exposed by the $this->db object
*/
//Executes Raw SQL and returns the result
$return = $this->db->execute('SHOW TABLES')->fetchAll();
//The below creates a Table 'Test'
/*$return = $this->db->createTable('Test',
array( 'id' => 'INT NOT NULL PRIMARY KEY AUTO_INCREMENT',
'Name' => 'VARCHAR(50)', 'Age' => 'INT' ) );*/
//The below inserts values into the table 'Test'
//$return = $this->db->insert( 'Test', array( 'Name' => 'Mac' ) );
//$return = $this->db->insert( 'Test', array( 'Name' => 'Adam' ) );
//The below deletes a record from Table 'Test' with id 1
//$return = $this->db->delete( 'Test', array('id' => '1' ) );
//The below selects all rows from the table 'Test'
//$return = $this->db->select( 'Test' );
//The below generates a CSV string from the result of a select query
$csvString = $this->db->generateCSV($return);
//The below is a utility function that force downloads the buffer
//with a file name 'test.csv'
Master::forceDownloadData($csvString, 'test.csv');
//The below drops the table 'Test'
//$return = $this->db->dropTable('Test');
//This retuns the value of the $result variable
return $return;
}