Codeigniter-创建自己的助手 [英] Codeigniter - Creating own helper

查看:45
本文介绍了Codeigniter-创建自己的助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建自己的帮助器.首先,我创建一个名为HelperModelBase的文件.

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

这是一个抽象类.

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);
    }

并创建一个名为select的方法

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)

问题从这里开始,因为codeigniter有一个特殊的代码来处理在何处进行操作.

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();

谢谢.

最后我来这个.

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 ?

推荐答案

我建议您看看

CodeIgniter用户指南:模型

使用CodeIgniter,对数据库的调用是在模型中进行的,而不是在助手中进行的.

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

然后,如果您的 $ where 是一个关联数组(实际上,如果我必须很好地阅读您的代码),则可以执行 foreach 循环来分配 where 子句和值.

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.

这篇关于Codeigniter-创建自己的助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆