~desarrollokumbia/kumbia/ActiveRecord

« back to all changes in this revision

Viewing changes to db_pool/db_query.php

  • Committer: Deivinson Tejeda
  • Date: 2010-01-24 20:53:08 UTC
  • Revision ID: deivinsontejeda@gmail.com-20100124205308-hsovf0apo3pbg3s0
Mejorando el chain where, quedan ahora los metodos where() y whereOr y se puede concatenar los where, ademas se crea un método interno para el DbQuery quien ira armando el los where en el orden como se vayan llamando y para dejar mas facil el where y whereOr solo reciben string asi no liamos a los usarios con array, sin embargo seguimos evaluando
Eje...
    public function consulta()
    {
        $this->get()->where('id < :id2')
                    ->where('id > :id')
                    ->bind(array(':id'=>2, ':id2'=>5));
        return $this->find();
    }
la consulta generada del anterios chain es:
SELECT * FROM prueba WHERE  (id < :id2)  AND (id > :id)

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
    /**
45
45
     * Clausula WHERE con AND
46
46
     *
47
 
     * @param array $conditions condiciones AND
 
47
     * @param string $conditions condiciones AND
48
48
     * @return DbQuery
49
49
     */
50
 
    public function whereAnd($and) 
 
50
    public function where($conditions) 
51
51
    {
52
 
        $where = array();
53
 
        //para consultas AND
54
 
        if(is_array($and)){
55
 
            foreach ($and as $k) {
56
 
                $where[] = $k;
57
 
            }
58
 
        }
59
 
        
60
 
        $this->_sql['where']['and'] = $where;
 
52
        $this->_sql['where'][] = $this->_where($conditions);
61
53
        return $this;
62
54
    }
63
55
    /**
64
56
     * Clausula WHERE con OR
65
57
     * 
66
 
     * @param array $or condiciones OR
67
 
     * return DbQuery
68
 
     */
69
 
    public function whereOr($or)
70
 
    {
71
 
        $where = array();
72
 
        //para consultas OR
73
 
        if(is_array($or)){
74
 
            foreach ($or as $k) {
75
 
                $where[] = $k;
 
58
     * @param string $conditions condiciones OR
 
59
     * @return DbQuery
 
60
     */
 
61
    public function whereOr($conditions)
 
62
    {
 
63
        $this->_sql['where'][] = $this->_where($conditions, FALSE);
 
64
        return $this;
 
65
    }
 
66
    /**
 
67
     * Método interno para crear la Clusula WHERE
 
68
     * 
 
69
     * @param string $conditions
 
70
     * @param bool   $type TRUE = AND; FALSE = OR
 
71
     * @return string clausula
 
72
     */
 
73
    protected function _where($conditions, $type=TRUE)
 
74
    {
 
75
        $cond=NULL;
 
76
        if(isset($this->_sql['where'])){
 
77
            if($type===TRUE){
 
78
                $cond = ' AND ';
 
79
            }else{
 
80
                $cond = ' OR ';
76
81
            }
77
82
        }
78
 
        $this->_sql['where']['or'] = $where;
79
 
        return $this;
 
83
        return $cond . "($conditions)";
80
84
    }
81
85
    /**
82
86
     * Parámetros que seran enlazados a la setencia SQL