Bonjour à tous,

Tout d'abord je tiens à remercier Grafikart

j'ai ajouté d'autre option à la fonction find de model commme
des opérateurs :
– de comparaison (>, =, <, >=, <=, <>)
– intégrés (BETWEEN, IN, LIKE, IS NULL).

  • jointure.
  • order by , group by

mais j'ai trouve cet solution n'ai pas efficace

j'ai trouvé problème dans les opérateurs logiques (NOT, AND ou OR) pour ajouté dans la fonction

si existe un autre solution efficace et mrc pr tt °(^-^)°

public function find($req = array())
    {

        $sql = 'SELECT ' ;
        if (isset($req'fields'])) {
            if(is_array($req'fields'])){
                $sql .= implode(', ',$req'fields']);
            }else{
                $sql .= $req'fields'];
            }
        }else{
            $sql .= '*';
        }
        $condj ='' ;
        if(isset($req'join'])) {
            if(is_array($req'join']'table'])){
                $cond = array();
                foreach ($req'join']'table'] as $k => $v) {
                    $cond] = "$k as $v";
                }
                $sql .= ' FROM '.$this->table.' as '.get_class($this).' , '.implode(', ',$cond).' ';
            }else{
                $sql .= ' FROM '.$this->table.' as '.get_class($this).' , '.$req'join']'table'].' ';
            }
            // Construction de la condition join
            if (isset($req'join']'conditions'])) {
                if(!is_array($req'join']'conditions'])){
                    $condj .=$req'join']'conditions'];
                }else{
                    $cond = array();
                    foreach ($req'join']'conditions'] as $k => $v) {
                        $cond] = "$k = $v";
                    }
                    $condj .= implode(' AND ', $cond);
                }
            }
        }else{
            $sql .= ' FROM '.$this->table.' as '.get_class($this).' ';
        }

        // Construction de la condition
        if (isset($req'conditions'])) {
            if(!empty($condj)){
                $sql .='WHERE '.$condj.' AND ' ;
            }else{
                $sql .='WHERE ' ;
            }

            if(!is_array($req'conditions'])){
                $sql .=$req'conditions'];
            }else{
                $cond = array();
                foreach ($req'conditions'] as $k => $v) {
                    if(is_array($v)){
                        switch ($v'cond']) {
                            case 'like':
                                $cond] = get_class($this).'.'.$k.' LIKE ("'.$v'value'].'")';
                            break;
                            case 'between':
                                $cond] = get_class($this).".$k BETWEEN ".$v'value']'v1'].' AND '.$v'value']'v2'];
                            break;
                            case 'in':
                                $cond] = get_class($this).".$k IN (".implode(', ',$v'value']).')';
                            break;
                            case 'not_in':
                                $cond] = get_class($this).".$k NOT IN (".implode(', ',$v'value']).')';
                            break;
                            case 'is_null':
                                $cond] = get_class($this).".$k IS NULL ";
                            break;
                            case '<>':
                                $cond] = get_class($this).'.'.$k.' <> '.$v'value'];
                            break;
                            case '>=':
                                $cond] = get_class($this).'.'.$k.' >= '.$v'value'];
                            break;
                            case '<=':
                                $cond] = get_class($this).'.'.$k.' <= '.$v'value'];
                            break;
                            case '>':
                                $cond] = get_class($this).'.'.$k.' > '.$v'value'];
                            break;
                            case '<':
                                $cond] = get_class($this).'.'.$k.' < '.$v'value'];
                            break;
                        }
                    }else {
                        if(!is_numeric($v)){
                            $v = '"'.mysql_escape_string($v).'"';
                        }
                        $cond] = get_class($this).".$k = $v";
                    }
                }
                $sql .= implode(' AND ', $cond);
            }
        }else{
            if(!empty($condj)){
                $sql .='WHERE '.$condj.' ' ;
            }
        }
        // Construction de la groupe 
        if (isset($req'group'])) {
            $sql .=' GROUP BY ';
            if(!is_array($req'group'])){
                $sql .=$req'group'];
            }else{
                $cond = array();
                foreach ($req'group'] as $k ) {
                    if(!is_numeric($v)){
                        $v = '"'.mysql_escape_string($v).'"';
                    }
                    $cond] = "$k";
                }
                $sql .= implode(' , ', $cond);
            }
        }
        // Construction de la order 
        if (isset($req'order'])) {
            $sql .=' ORDER BY ';
            if(!is_array($req'order'])){
                $sql .=$req'order'];
            }else{
                $cond = array();
                foreach ($req'order'] as $k ) {
                    if(!is_numeric($v)){
                        $v = '"'.mysql_escape_string($v).'"';
                    }
                    $cond] = "$k";
                }
                $sql .= implode(' , ', $cond);
            }
        }
        if (isset($req'limit'])) {
            $sql .=' LIMIT '.$req'limit'];
        }
        $pre = $this->db->prepare($sql);
        $pre->execute();
        return $pre->fetchAll(PDO::FETCH_OBJ);
    }

exemple :

function test($num = null){
        $this->loadModel('Post');

                /* exemple intégrés
        return $this->Post->find(array(
                'conditions' => array('created' => array('cond' => 'between' , 'value' => array('v1' => '"2011/10/13"','v2' => '"2011/10/16"')
                    ))
            ));
                */
        /* exemple jointure
        return $this->Post->find(array(
                'fields' => 'u.login , Post.created , g.name',
                'conditions' => array('id' => $num ),
                'join' => array('table' => array( 'users' => 'u','`group`' => 'g') , 
                                'conditions' =>array('u.id' => 'Post.user_id' ,'g.id' => 'u.group_id')
                                )
            ));
        */
    }

1 réponse


Grafikart
Réponse acceptée

A ce niveau là regarde comment font les frameworks MVC tu trouvera de bonnes idées en regardant le code source d'autres développeurs.