To load a model belonging to our component from its controller:
$modelo = $this-> loadModel('modelo');
ROOT_COM.DS.'models' *
(*) ROOT_COM refers to the component's path.
To load a model belonging to another component from the controller:
$modelo = $this-> loadModelFromOtherComponent('modelo','componente');
The database connection is not loaded by default, since not every model needs data access. To give a model access to the database, put the following code in its constructor:
public function__construct() {
parent::__construct();
$this->cargaBD();
}
The database could be loaded in any other controller method; it need not be in the constructor. It all depends on the requirements.
Example of a method using PDO:
public function getSecciones(){
try {
$secciones = $this->_db->query("SELECT * FROM secciones")
return $secciones->fetchAll();
}
catch (PDOException $e) {
echo $e->getMessage();
return false;
}
}