CodeIgniter是否支持命名查询绑定?

时间:2012-07-16 23:52:31

标签: php sql codeigniter

我检查了CI手册,发现它支持这种查询绑定:

$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3, 'live', 'Rick')); 

但我更喜欢PDO风格:

$sql = "SELECT * FROM some_table WHERE id = :id AND status = :status  AND author = :author";

通过这种方式,绑定可以更灵活,顺序也无关紧要。

CI是否支持这种绑定?

3 个答案:

答案 0 :(得分:1)

默认情况下,Codeigniter不支持命名绑定。

因为它使用命名绑定更具可读性,所以我“入侵”了数据库类,以便它接受命名或未命名的绑定。

如果有人对“hack”感兴趣,请告诉我,我会将其上传到某个地方

答案 1 :(得分:0)

您可以使用命名查询绑定。让我举个例子。

$sql = 'SELECT @column FROM @table LIMIT @limit';
$this->db->bind('@column', 'username');
$this->db->bind('@table', 'users');
$this->db->bind('@limit', 10);
$query = $this->db->query($sql);  

query binding

答案 2 :(得分:0)

我扩展了CI_Model以按名称执行参数查询。

class MY_Model extends CI_Model{

    protected $db;
    private $sql;
    private $bind_marker;

    public function __construct() {
        $this->load->database($this->setConfigCliente());
        $CI =& get_instance();
        $this->db = $CI->db;
        $this->bind_marker = $this->db->bind_marker;
        parent::__construct();
    }


    public function setConfigCliente() {
//        $db['hostname'] = 'localhost';
//        $db['dsn'] = "mysql:host=localhost;dbname=".$this->getDbName();
        $db['hostname'] = "mysql:host=localhost;dbname=".$this->getDbName();
        $db['username'] = 'root';
        $db['password'] = 'pass#######';
        $db['database'] = $this->getDbName();
//        $db['dbdriver'] = 'mysqli';
        $db['dbdriver'] = 'pdo';
        $db['dbprefix'] = '';
        $db['pconnect'] = TRUE;
        $db['db_debug'] = TRUE;
        $db['cache_on'] = FALSE;
        $db['cachedir'] = '';
        $db['char_set'] = 'utf8';
        $db['dbcollat'] = 'utf8_general_ci';
        $db['swap_pre'] = '';
        $db['autoinit'] = TRUE;
        $db['stricton'] = FALSE;

        return $db;


    }

    final protected function query($sql, $bind=false, $return_object = true){
        $this->sql = $sql;
        if(is_array($bind) and count($bind)>0){
            $bind = $this->process_bind($bind);
        }
        $query = $this->db->query($this->sql, $bind, $return_object);
        return $query;
    }

    private function process_bind($bind){
        $bindOrder = null;
        $bindList = null;

        $pattern = "/[^']:[A-Za-z0-9_]+[^']/";
        $preg = preg_match_all($pattern, $this->sql, $matches, PREG_OFFSET_CAPTURE);
        if($preg !== 0 and $preg !== false){
            foreach($matches[0] as $key=>$val){
                $bindOrder[$key] = trim($val[0]);
            }
            foreach($bindOrder as $field){
                $this->sql = str_replace($field, $this->bind_marker, $this->sql);
                $bindList[] = $bind[$field];
            }
        }else{
            $bindList = $bind;
        }

        return $bindList;
    }

    private function getDbName(){
        /*
         * LISTA DE NOME DE BANCO SEGUNDO O SUBDOMINIO.
         */

        //AMIG
        $sub['amig']['dbName'] = 'amig';
        $sub['10']['dbName'] = 'amig';

        //SISTEMA.ARKSYS
        $sub['sistema']['dbName'] = 'sistema';
        $sub['code']['dbName'] = 'sistema';


        $host = explode('.', $_SERVER['HTTP_HOST']);
        $subDom = $host[0];
        if(isset($sub[$subDom])){
            return $sub[$subDom]['dbName'];
        }else{
            return null;
        }

    }


}

使用示例:

class Login_usuarios extends MY_Model {

    public function __construct() {
        parent::__construct();
    }

    public function get_login_dados($email, $senha) {

        $sql = "
        SELECT
            u.usu_id,
            u.usu_nome,
            u.usu_alterar_senha,
            u.usu_situacao,
            (SELECT log_data FROM log_usuario_acesso l WHERE l.usu_id = u.usu_id ORDER BY l.log_data DESC LIMIT 1) AS data_ultimo_acesso,
            (SELECT
                p.par_nome_fantasia
            FROM 
                sys_parametros p
            WHERE
                p.par_id = 1) as par_nome_fantasia
        FROM 
            cad_usuarios u
        WHERE
            u.usu_email = :usu_email
        AND
            u.usu_senha = :usu_senha
        AND
            u.usu_situacao <> :usu_situacao";

        $bind[':usu_senha'] = $senha;
        $bind[':usu_email'] = $email;
        $bind[':usu_situacao'] = 'Descartado';

        $query = $this->query($sql, $bind);

        return $query->result_array();
    }
}

仅由“:”

识别的参数