2

I would like to get your MVC experience on the following:

I have a table where I say which user_id is in which group_id and a second table where I say which user_id has which user_name.

Now I want a function to which I pass a group_id and it gives me all user_names in the group.

The question is, what does the Controller do and what the Model:

  1. Controller calls a Model (get_user_ids_from_group) that returns the user ids and then the controler calls different model (get_user_name_by_id) to return the usernames.

  2. A controller calls a Model (`get_user_names_from_group) and the model internally gets first the user ids and then the user names.

I understand that the first way is more MVC strict. But how strict would you be in a case like this? Right now I am always being very strict.

As a result my model functions have always just two lines (query and return) and my controller are bigger. So to make controller and model size more equal the second option would be possible.

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
almo
  • 6,107
  • 6
  • 43
  • 86

3 Answers3

2

Actually, it has nothing to do with MVC, as CodeIgniter doesn't implement MVC, but a sort of MVP.

However, if you're using RDBMS such as MySQL, you could JOIN these two table (within the Model) and fetch the result (within the Controller) as I suggested in a similar topic on SO.

application/models/user.php

class User extends CI_Model
{
    public function get_users_by_group($group_id)
    {
        $this->db->select('*')->from('groups');
        // While group_id and user_id have a N:1 relation
        $this->db->where('group_id', $group_id);
        $this->db->join('users', 'users.user_id = groups.user_id');
        $query=$this->db->get();
        return $query->result_array();
    }
}

Then fetch and pass the result within the Controller:

application/controllers/users.php

class Users extends CI_Controller
{
    public function view($group_id)
    {
        $this->load->model('user');
        // Fetch the result from the database
        $data['users'] = $this->user->get_users_by_group($group_id);
        // Pass the result to the view
        $this->load->view('users_view', $data);
    }
}
Community
  • 1
  • 1
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
0

In MVC model should not be only use for storing and getting the data but also it should contain the business logic.

Controller - Should take input communicate with model and view

Model - Should contain business logic and data store

View - Only for Output

So its like Input -> Process -> Output

Also its depend on you where to put what and you should have a balance code on controller and model, Not write everything in controller or in model

In you case I think you should uses get_user_names_from_group and just pass the group name and make a join query between two table. And when you define your function in the model its give you the option to reuse the function again in case you need same thing on next controller.

Minhaz
  • 446
  • 5
  • 7
0

Common model example:

 <?php
    class Common_model extends CI_Model {

        function get_entry_by_data($table_name, $single = false, $data = array(), $select = "", $order_by = '', $orderby_field = '', $limit = '', $offset = 0, $group_by = '') {

            if (!empty($select)) {
                $this->db->select($select);
            }

            if (empty($data)) {

                $id = $this->input->post('id');

                if (!$id)
                    return false;

                $data = array('id' => $id);
            }
            if (!empty($group_by)) {

                $this->db->group_by($group_by);
            }


            if (!empty($limit)) {
                $this->db->limit($limit, $offset);
            }

            if (!empty($order_by) && !empty($orderby_field)) {

                $this->db->order_by($orderby_field, $order_by);
            }

            $query = $this->db->get_where($table_name, $data);

            $res = $query->result_array();

            //echo $this->db->last_query();exit;

            if (!empty($res)) {

                if ($single)
                    return $res[0];
                else
                    return $res;
            } else
                return false;
        }


    public function get_entry_by_data_in($table_name, $single = false, $data = array(), $select = "", $order_by = '', $orderby_field = '', $limit = '', $offset = 0, $group_by = '',$in_column='',$in_data=''){



            if (!empty($select)) {
                $this->db->select($select);
            }

            if (empty($data)) {

                $id = $this->input->post('id');

                if (!$id)
                    return false;

                $data = array('id' => $id);
            }
            if (!empty($group_by)) {

                $this->db->group_by($group_by);
            }


            if (!empty($limit)) {
                $this->db->limit($limit, $offset);
            }

            if (!empty($order_by) && !empty($orderby_field)) {

                $this->db->order_by($orderby_field, $order_by);
            }

            if (!empty($in_data) and !empty($in_column)) {
               $this->db->where_in($in_column,$in_data);
            }

            $query = $this->db->get_where($table_name, $data);

            $res = $query->result_array();

            //echo $this->db->last_query();exit;

            if (!empty($res)) {

                if ($single)
                    return $res[0];
                else
                    return $res;
            } else
                return false;

    }
        public function getAllRecords($table, $orderby_field = '', $orderby_val = '', $where_field = '', $where_val = '', $select = '', $limit = '', $limit_val = '') {

            if (!empty($limit)) {
                $offset = (empty($limit_val)) ? '0' : $limit_val;
                $this->db->limit($limit, $offset);
            }
            if (!empty($select)) {

                $this->db->select($select);
            }
            if ($orderby_field)
                $this->db->order_by($orderby_field, $orderby_val);

            if ($where_field)
                $this->db->where($where_field, $where_val);

            $query = $this->db->get($table);

            //return $query->num_rows;
            //echo $this->db->last_query();
            if ($query->num_rows > 0) {
                return $query->result_array();
            }
        }

        function alldata($table) {
            $query = $this->db->get($table);
            return $query->result_array();
        }

        function alldata_count($table, $where) {
            $query = $this->db->get_where($table, $where);
            return $query->num_rows();
        }

        function insert_entry($table, $data) {
            $this->db->insert($table, $data);
            return $this->db->insert_id();
        }



        function update_entry($table_name, $data, $where) {
            return $this->db->update($table_name, $data, $where);
        }

      public function get_data_by_join($table, $table2, $where, $table1_column, $table2_column, $limit = '', $order_column = '', $order_by = 'DESC', $select_columns = '', $is_single_record = false, $group_by = '', $join_by = '', $offset = '') {
            if (!empty($select_columns)) {
                $this->db->select($select_columns);
            } else {
                $this->db->select('*');
            }

            $this->db->from($table);
            $this->db->join($table2, $table . '.' . $table1_column . '=' . $table2 . '.' . $table2_column, $join_by);
            $this->db->where($where);
            if (!empty($limit)) {
                if (!empty($offset)) {
                    $this->db->limit($limit, $offset);
                } else {
                    $this->db->limit($limit);
                }
            }
            if (!empty($order_column)) {
                $this->db->order_by($order_column, $order_by);
            }

            if (!empty($group_by)) {
                $this->db->group_by($group_by);
            }

            $query = $this->db->get();
            if ($query->num_rows() > 0) {

                if ($is_single_record) {
                    $rs = $query->result_array();
                    return $rs[0];
                } else {
                    return $query->result_array();
                }
            } else {
                return false;
            }
        }

        function DeleteRecord($table_name, $where) {
            return $this->db->delete($table_name, $where);
        }

        function managerecord() {
            $count = 1;
            $where = array('channel_id' => 8,
                'field_id_12 !=' => '');
            $this->db->where($where);
            $query = $this->db->get('channel_data');
            $data = $query->result_array();
            foreach ($data as $value) {
                $id = $value['field_id_12'];
                $update = array('telephone' => $value['field_id_53'],
                    'about' => $value['field_id_54'],
                    'license' => $value['field_id_18'],
                    'broker' => $value['field_id_19'],
                    'preferred' => 'yes');
                $this->db->update('members', $update, array('member_id' => $id));
            }
            echo "done1";
        }

        public function get_content_landing_page($table = false, $field = false, $id = false, $id_name = false) {
            $this->db->select($field);
            $this->db->from($table);
            $this->db->where($id_name, $id);
            $query = $this->db->get();
            return $query->result_array();
        }

        public function get_field_landing_page($id = false,$fields=false) {
            $this->db->select($fields);
            $this->db->from('terratino_form_fields_master');
            $this->db->where('id', $id);
            $query = $this->db->get();
            return $query->result_array();
        }

    }


    ?>
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
govind
  • 1
  • 2
  • This answer is missing its explanation. "Code dumps" are low-value on Stackoverflow because they do very little to educate/empower the OP and thousands of future researchers. Please be more generous when "sharing your stack". – mickmackusa Mar 19 '20 at 04:54