-3

Please any body can help.

The right procedure for methods(functions) using in codeigniter.

I mean that to write separate method for every task in model or to use one method that is used for general purpose. i.e

1:-One approach

  public function insert_records($tabel, $data){
        return $this->db->insert($tabel, $data);
    }

2:-Second approach

public function insert_user(){
     return $this->db->insert('users', array('name'=>$this->input->post('name'),'email'=>$this->input->post('email'));
    }

Same for teacher

public function insert_teacher(){
         return $this->db->insert('teachers', array('name'=>$this->input->post('name'),'email'=>$this->input->post('email'));
        }

Please give the right solution that to use separate functions is best or to pass data and table name in one functions is best.? if any one have no proper solution please leave for expert one's so that to give the right solution.

smehsoud
  • 312
  • 2
  • 11
  • try http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc/5864000#5864000 – Linus Sep 06 '16 at 09:39

1 Answers1

-1

Your question is a little broad, but I will try to answer as well as I can.

Firstly, PHP with MVC implementations is an outdated approach. On modern solutions (with support of Angular or React) you completely move the View part away from PHP, leaving it with application logic, data and a simple API. This is a MVVM pattern, which results in an overall better user experience.

Secondly, to answer your questions:

  1. Direct access to $_GET, $_POST and $_REQUEST can be blocked during initiation of your script. Your router should read the data and then use unset() to prevent any further direct access.
  2. Use an ORM. You should not write SQL queries unless it is required due to some performance reasons in very specific cases.
  3. Do a code review. Using proper GIT flow will allow you to check other developers' changes before they are merged. This will give you some space to discuss your coding styles and come up with a set of rules everyone will obey.
  4. Again, use an ORM. That would make absolutely clear that protected class User can not be used for insertions. Only class Teacher extends User and it's insert() can be used to insert data.
Boris Schegolev
  • 3,601
  • 5
  • 21
  • 34
  • The main purpose of my question was that use of separate methods for every process in controller or model are best.or the way to use multipurpose methods by which we can handle many tasks only by passing different parameters. – smehsoud Sep 06 '16 at 09:04
  • Simply you can imagine that as there are no hard rules in mvc while coding some one can call model in view that works but is not good practice.So the best way to use code in proper way? – smehsoud Sep 06 '16 at 09:07
  • I myself working on codeigniter so have the knowledge of it – smehsoud Sep 06 '16 at 09:08
  • Multipurpose methods may be used for internal optimization, but should be private and never appear on the "interface". Actually, proper domain design may help you a lot when deciding about visibility of methods. – Boris Schegolev Sep 06 '16 at 09:10
  • 2
    `unset`ting superglobals is a huge wtf. Also "use an ORM" is a blanket statement and blanket statements are mostly not based on actual investigation into the domain. – PeeHaa Sep 06 '16 at 09:52
  • @PeeHaa Using globals, including superglobals, was abandoned in PHP a decade ago. Whoever reads `$_POST` in the middle of the code should really re-think the approach. As for ORM, the answer simply matches the question :) – Boris Schegolev Sep 06 '16 at 10:41
  • 1
    So don't do that!? Overwriting the superglobals is a terribad idea because people / code expects it to be there. You gain nothing by unsetting it besides annoying people / breaking code. – PeeHaa Sep 06 '16 at 10:43
  • 2
    Your approach amounts to: "people shouldn't drive too fast so I removed all wheels from all vehicles" – PeeHaa Sep 06 '16 at 10:44
  • That is exactly same approach as when register_globals was turned off in PHP 4.2.0. A lot of people could disagree with that, a lot of people got annoyed and a lot of code got "broken". But it was the right way to go. – Boris Schegolev Sep 06 '16 at 10:54
  • 2
    This is nothing like `register_globals` at all. `register_globals` was a potential security issue waiting to happen because it overwrites / initializes variables without knowing where the data is coming from. Enabling innocent users to use potential dangerous data without them realizing it. – PeeHaa Sep 06 '16 at 13:30