Welcome Guest, Not a member yet? Register   Sign In
New to CodeIgniter--need some help incorporating some existing classes
#1

[eluser]jprateragg[/eluser]
I've developed an application for my company that analyzes financial information. The bulk of this application consists of three classes: one parent class and two child classes.

Code:
//retrieves "meta" information about an analysis
class Analysis {
    public $analysis_id;
    public $db;

    public function __construct($db, $analysis_id) {
        $this->analysis_id = $analysis_id;
        $this->db = $db;
    }
}

//retrieves specific information about a standard-type analysis
class Standard extends Analysis {
    public $analysis_id;
    public $db;

    public function __construct($db, $analysis_id) {
        $this->analysis_id = $analysis_id;
        $this->db = $db;
    }
}

//retrieves specific information about a consolidated-type analysis
class Consolidated extends Analysis {
    public $analysis_id;
    public $db;

    public function __construct($db, $analysis_id) {
        $this->analysis_id = $analysis_id;
        $this->db = $db;
    }
}

At the top of my pages, I initiate the Analysis class, then initiate one of the child classes depending on the type of analysis the selected analysis is (each class has the same methods, but each have different calculations).

Is it possible to incorporate this into a new CodeIgniter project? Would these be a custom library? A model? How would I go about initializing the correct library--I must initiate a new Standard or Consolidated object based on the type of analysis that's selected. I like the layout of CodeIgniter and feel it would be better suited for my needs, but I want to make sure I can incorporate this still. Thanks!
#2

[eluser]solid9[/eluser]
/application/core/
MY_Controller
Code:
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
    public $analysis_id;
    public $db;

    public function __construct($db, $analysis_id) {
        $this->analysis_id = $analysis_id;
        $this->db = $db;
    }
}

/application/controller/
Standard
Code:
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
class Standard extends MY_Controller
{
    public $analysis_id;
    public $db;

    public function __construct($db, $analysis_id) {
        $this->analysis_id = $analysis_id;
        $this->db = $db;
    }    
}

/application/controller/
Consolidated
Code:
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
class Consolidated extends MY_Controller
{
    public $analysis_id;
    public $db;

    public function __construct($db, $analysis_id) {
        $this->analysis_id = $analysis_id;
        $this->db = $db;
    }
}

Not tested but try.
#3

[eluser]solid9[/eluser]
Standard and Consolidated classes are optional.
You can put them in /application/library/ if you like.
#4

[eluser]jprateragg[/eluser]
So I'm assuming MY_Controller would contain the methods and properties for my Analysis class, correct? Would it be better to make the Standard and Consolidated class libraries instead? I may need to use them in different controllers. Since these classes are primarily responsible for generating values from a database, would they be better off as models instead? Or libraries?
#5

[eluser]solid9[/eluser]
If Standard and Consolidated classes are for database querying.
Then better use MY_Model instead of MY_Controller

Here watch a video tutorial regarding MY_Model.
http://codeigniter.tv/videos/keywords/MY_Model
#6

[eluser]kostyak[/eluser]
Hi, jprateragg!
Recently I had deal with question like yours.
And what I am thinking about it now.
According to the MVC pattern, all your programm’s logic must be in a model. And controller must play role to interaction between user and model through views.
So, for you will be the best to put all your classes to models.
for instance:
1. Make file MY_Model.php and put it to the ‘application/core/’
Code:
<?php
/**
*retrieves "meta" information about an analysis
*/
class MY_Model extends CI_Model{

    public $analysis_id;
    public $db;

    public function __construct($db, $analysis_id) {
  parent::__construst();
  $this->analysis_id = $analysis_id;
  $this->db = $db;
    }

}
2. Make 2 other files (and put them to ‘application/models/’):
a. Standard_model.php:
Code:
<?php
class Standard_model extends MY_Model{
//put your code there
}
b. Consolidated_model.php:
Code:
<?php
class Consolidated_model extends MY_Model{
//put your code there
}
#7

[eluser]jprateragg[/eluser]
Thanks solid9 and kostyak for your suggestions! I have decided to put the main Analysis class in MY_Model, create a Standard and Consolidated model (in application/models), and have those classes extend the MY_Model class instead of CI_Model. This way in my controllers and I just load the Standard or Consolidated model and have access to all of the meta models. Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB