Codeigniter - Creating own helper

妖精的绣舞 提交于 2019-12-24 21:43:43

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/9580454/codeigniter-creating-own-helper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!