Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to write Model code in Codeigniter

I am very much confused regarding which technique to use to code Model in codeigniter.

I searched a lot and found various methods used by various coders. Kindly guide me the best way to write Model Class using codeigniter.

Few examples;

  • Some take long parameters in Method signature

     function insert_data($name, $address, $email, $....,$...,$..)
    

    While others just use the following and write all post code inside Model..

    function insert()
    { 
    $name= $this->input->post('var1');
    $name= $this->input->post('var1');
    $name= $this->input->post('var1');
    }
    
  • Some create different Model for each Table, while some write very few Models..

    And many more contradictions between different developers, I am so much confused in which technique to use to write more efficient and industry standard code.

    Kindly guide me Thanks a lot

like image 371
dev90 Avatar asked Jun 29 '15 12:06

dev90


People also ask

How do you create a CI model?

Syntax (call model method) –$this->[Model-class-name]->method-name(); Create a User. php file in application/controllers directory. Load above created Main_model using $this->load->model('Main_model') method in the __construct() method and call getUsers() method using $this->Main_model->getUsers() .

How do you define a model in CI?

Auto-loading Models If you find that you need a particular model globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload. php file and adding the model to the autoload array.

Does CodeIgniter use MVC?

Like most of the web frameworks, CodeIgniter uses the Model, View, Controller (MVC) pattern to organize the files. This keeps the data, the presentation, and flow through the application as separate parts.

How check model loaded or not in CodeIgniter?

You can use the log_message() function.


2 Answers

This is a good question. It goes back on the pros and cons of Codeigniter. Being that you can customize Codeitgniter as much as you wish (which a lot of people overdo), issues like these come up, where its up to the developer to decide where things belong. With that being said, I will share how I use models.

According to Codeigniter's MVC standards:

The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.

Therefore a model should only handle the communication with your database, or if you don't use databases, it should handle the retrieval of data from other sources (i.e. json, text files, etc...).

What I recommend:

  • Keep all your internal functionalities in your controller method, this includes all your $this->input->post() information. Once you have all your post() information, create the necessary array and pass it to the model
  • The model, should receive the array (which you constructed in your controller) and build the database queries needed to retrieve the information.

With this structure, you may reuse these model methods from any controllers, keeping a modular structure.

Hope this helps.

like image 161
CodeGodie Avatar answered Nov 14 '22 23:11

CodeGodie


this might help you. (how i use model in codeigniter)

MY MODEL PHP FILE:

<?php

class Dbmodel extends CI_Model {

function __construct() {
    parent::__construct();
    $this->db->query("SET time_zone='+05:30'");
}

function customQueryInsert($table, $array_data, $array_condition) {
    if (!empty($array_condition)) {
        $this->db->where($array_condition);
    }
    $result = $this->db->insert($table, $array_data);
    if ($result) {
        return TRUE;
    } else {
        return FALSE;
    }
}

function customQueryInsertGetId($table, $array_data, $array_condition) {
    if (!empty($array_condition)) {
        $this->db->where($array_condition);
    }
    $result = $this->db->insert($table, $array_data);
    if ($result) {
        return $this->db->insert_id();
    } else {
        return FALSE;
    }
}

function lastQuery() {
    return $this->db->last_query();
}
}

AND CALLING IN CONTROLLER:

$this->load->model('Dbmodel', 'Dbmodel', TRUE);
$this->notification = $this->Dbmodel->customQuerySingleResult("select count(id) as ncount from notification where id='$this->id' and status='unread'", "ncount");
$this->Dbmodel->customQueryInsert("activity", array('userid' => $this->id, 'username' => $this->email, 'activity' => uri_string()), array());

this is how i use MODEL in CODEIGNITER.. in your case you can use below code to insert your data, assuming you are writing it inside a controller method.

$this->load->model('Dbmodel', 'Dbmodel', TRUE);
$status = $this->Dbmodel->customQueryInsert("table_name",array('column_name'=>'var1','column_two'=>'var2'));
if($status){
  //do succes stuffs
}else{
  //fail redirect...
}

now you dont need to create methods with required parameters.. just call customQueryInsert method and pass table name as first parameter and array of column_name=>column_data to second parameter...

you can optimize it according to your need.. or if you want further help please let me know..

like image 31
noobdeveloper99 Avatar answered Nov 14 '22 21:11

noobdeveloper99