Welcome Guest, Not a member yet? Register   Sign In
Gotta be an easier way of doing this...
#1

[eluser]coding_hero[/eluser]
CI newbie here, I'm trying to retrain my brain into working MVC. So, this may be a "stupid question".

The problem I'm dealing with is a simple one. Let's use the common example of an employee database with the fields:

first_name,
last_name,
employee_number,
birthdate,
salary

It's easy enough to make a CI page to display as little or as much of this as I want to. But, what if I want to query it in many different ways? Is it a model if I want to query by employee_number (example: employee_number in a range), a different model for querying by salary (example: salary less than or greater than an amount), and yet another model for querying by last_name (example: all last names beginning with 'N')?

Thanks for your help!
#2

[eluser]daBayrus[/eluser]
only one model, say, employee_model. you will have to create different functions for your various queries...
#3

[eluser]underskor[/eluser]
Code:
class Employee_model extends Model {
  var $id;
  var $name;
  var $salary;
  //etc

  function fetch_employee_data() {
    if($this->id !== NULL) { $this->db->where('id', $this->id); }
    if($this->name !== NULL) { $this->db->where('name', $this->name); }
    if($this->salary !== NULL) { $this->db->where('salary', $this->salary); }

    $this->db->from('employees'); //etc, return data however you like
  }

Then in your controller just do something like:
Code:
function view_employee($id = NULL, $name = NULL, $salary = NULL) { //cant remember if this is valid or not for controllers (URI etc)
  $this->employee_model->id = $id;
  $this->employee_model->fetch_employee_data(); //will get employee data based on id, should be able to get the gist of things from here
}

Hope that helps out, at least until Colin Williams, Xwero or one of the other CI Guns shows up in here Smile
#4

[eluser]xwero[/eluser]
daBayrus got it right, just create methods for the different queries you need.

A model is an object that contains methods that fetch/manipulate similar data. The examples you gave are only changes in the where part of the sql statement.

There are people who follow the rule one model, one table too strict and then they create code to use model methods in another model which only adds fat to the code.
For instance if employees can buy things from the company and you need to display an overview of which employee bought what. You could be tempted to use the employee_model method that gets the employee name in the sales_model. There are two alternate solutions for this:

- add the employee name query to a sales_model method.
- extract the employee ids from the sales_model method results, get all the names, link the names to the sales_model method results

The last one is the best solution if you want your models to be as flexible as possible, if you have no intention of reusing the model the first alternative method will be the fastest because then you only need one sql statement.




Theme © iAndrew 2016 - Forum software by © MyBB