Mert METİN
Mert METİN

Reputation: 1288

Codeigniter - Creating own helper

I try to create my own helper. First i create a file called HelperModelBase.

This is a abstract class.

abstract class Pasaj_Model_Base {

    public $table_name;
    public $table_alias;
    public $class_name;
    public $lastSql;

    public function __construct() {
        $this->class_name = get_class($this);
        $this->table_name = strtolower($this->class_name);
        $this->table_name = str_replace('_dbview', '', $this->table_name);
    }

and create a method called select

public function select($where = null, $order = null, $limit = null, $columns = '*') {
        if (!$columns)
            $this->db->select('*');
        elseif (is_array($columns)) {
            $columns = implode(',', $columns);
            $this->db->select($columns);
        }



}

As you see above i do a simple select operation but , What i want to do is that i add one or more operation.

For example where and order by in order to do that. I add this to my method:

if($where)

Problem begins here because codeigniter has a special code to handle where operations.

$this->db->where();

how can i do that ? How can i that where situtation to my

$this->db->select();

Thank you .

Finally i come to this.

public function select($where = null, $order = null, $limit = null, $columns = '*') {
        if (!$columns)
            $this->db->select('*');
        elseif (is_array($columns)) {
            $columns = implode(',', $columns);
            $this->db->select($columns);
        }

        if($where) 
            $this->db->where($where);
        if($order)
            $this->db->order_by($order);
        if($limit)
            $this->db->limit($limit);

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

        return $query;
    }

i will notice about syntax of parameters . Can above code run ?

Upvotes: 0

Views: 522

Answers (1)

BMN
BMN

Reputation: 8508

I would suggest you to have a look at

CodeIgniter User Guide : Models

With CodeIgniter, the calls to the databases are made in the models, not in the helpers.

Then, if your $where is an associative array (in fact if I read your code well it have to), you can do a foreach loop to assign where clauses and values.

Upvotes: 1

Related Questions