From 5c59fefb8a23441f078110d1fc90c1e70882d5bc Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 23 Jul 2016 10:01:01 +0200 Subject: [PATCH] =?UTF-8?q?Gestion=20de=20plusieurs=20conditions=20pour=20?= =?UTF-8?q?un=20m=C3=AAme=20champ=20+=20gestion=20des=20conditions=20:=20'?= =?UTF-8?q?<',=20'<=3D',=20'>',=20'>=3D',=20'=3D',=20'LIKE'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manager/ORM/Rows.php | 60 +++++++++++++++++++++++++++++--------------- test/automate.php | 10 ++++---- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/manager/ORM/Rows.php b/manager/ORM/Rows.php index ebfc3ea..337c049 100644 --- a/manager/ORM/Rows.php +++ b/manager/ORM/Rows.php @@ -14,7 +14,7 @@ const COND_SUP = '>'; const COND_INFEQ = '<='; const COND_SUPEQ = '>='; - const COND_LIKE = '>='; + const COND_LIKE = 'LIKE'; /* Attributs */ private $where; // Tableau associatif contenant les conditions @@ -95,7 +95,12 @@ return $this; /* (5) Si type OK, on enregistre la condition */ - $this->where[$keys[0]] = $primary; + // {1} Si aucune condition pour ce champ, on crée un tableau // + if( !isset($this->where[$keys[0]]) ) + $this->where[$keys[0]] = []; + + // {2} On ajoute la condition // + $this->where[$keys[0]][] = $primary; /* [3] Si clé composée @@ -131,7 +136,12 @@ } /* (6) Si type OK, on enregistre la condition */ - $this->where[$key] = $primary[$i]; + // {1} Si aucune condition pour ce champ, on crée un tableau // + if( !isset($this->where[$key]) ) + $this->where[$key] = []; + + // {2} On ajoute la condition // + $this->where[$key][] = $primary[$i]; } @@ -210,7 +220,14 @@ /* [3] Si type OK, on enregistre la condition =========================================================*/ - $this->where[$column_name] = $a[0]; + /* (1) Si aucune condition pour ce champ, on crée un tableau */ + if( !isset($this->where[$column_name]) ) + $this->where[$column_name] = []; + + /* (2) On ajoute la condition */ + $this->where[$column_name][] = $a[0]; + + // On retourne l'object courant return $this; @@ -420,12 +437,14 @@ =========================================================*/ /* (1) On met les conditions locales */ $c = 0; - foreach($this->where as $field=>$value){ - if( $c == 0 ) $requestS .= 'WHERE '.$this->schema['table'].'.'.$field.' '.$value[1].' :'.$this->schema['table'].'_x_'.$field."\n"; - else $requestS .= 'AND '.$this->schema['table'].'.'.$field.' '.$value[1].' :'.$this->schema['table'].'_x_'.$field."\n"; + foreach($this->where as $field=>$conditions) - $c++; - } + foreach($conditions as $cdt=>$value){ + if( $c == 0 ) $requestS .= 'WHERE '.$this->schema['table'].'.'.$field.' '.$value[1].' :'.$this->schema['table'].'_x_'.$field.'_'.$cdt."\n"; + else $requestS .= 'AND '.$this->schema['table'].'.'.$field.' '.$value[1].' :'.$this->schema['table'].'_x_'.$field.'_'.$cdt."\n"; + + $c++; + } /* (2) On ajoute les jointures */ foreach($this->joined as $localField=>$rows){ @@ -436,12 +455,13 @@ /* (3) On ajoute les conditions des jointures */ foreach($this->joined as $rows) - foreach($rows->where as $field=>$value){ - if( $c == 0 ) $requestS .= 'WHERE '.$rows->schema['table'].'.'.$field.' '.$value[1].' :'.$rows->schema['table'].'_x_'.$field."\n"; - else $requestS .= 'AND '.$rows->schema['table'].'.'.$field.' '.$value[1].' :'.$rows->schema['table'].'_x_'.$field."\n"; + foreach($rows->where as $field=>$conditions) + foreach($conditions as $cdt=>$value){ + if( $c == 0 ) $requestS .= 'WHERE '.$rows->schema['table'].'.'.$field.' '.$value[1].' :'.$rows->schema['table'].'_x_'.$field.'_'.$cdt."\n"; + else $requestS .= 'AND '.$rows->schema['table'].'.'.$field.' '.$value[1].' :'.$rows->schema['table'].'_x_'.$field.'_'.$cdt."\n"; - $c++; - } + $c++; + } /* [6] On exécute la requête et 'bind' les paramètres @@ -451,20 +471,20 @@ $binded = []; /* (1) On bind tous les paramètres locaux */ - foreach($this->where as $field=>$value) - $binded[':'.$this->schema['table'].'_x_'.$field] = $value[0]; + foreach($this->where as $field=>$conditions) + foreach($conditions as $cdt=>$value) + $binded[':'.$this->schema['table'].'_x_'.$field.'_'.$cdt] = $value[0]; /* (3) On bind tous les paramètres des jointures */ foreach($this->joined as $rows) - foreach($rows->where as $field=>$value) - $binded[':'.$rows->schema['table'].'_x_'.$field] = $value[0]; + foreach($rows->where as $field=>$conditions) + foreach($conditions as $cdt=>$value) + $binded[':'.$rows->schema['table'].'_x_'.$field.'_'.$cdt] = $value[0]; /* [6] On exécute la requête et retourne le résultat =========================================================*/ - var_dump($requestS); - var_dump($binded); /* (1) On exécute la requête */ $request->execute($binded); diff --git a/test/automate.php b/test/automate.php index a2903c7..a14ebbf 100755 --- a/test/automate.php +++ b/test/automate.php @@ -348,13 +348,13 @@ $myUser = Table::get('user') // Access to table 'user' - ->getById([3, Rows::COND_SUP]) // PRIMARY KEY (if composed, all arguments in array) - // ->getById([10, Rows::COND_INF]) // PRIMARY KEY (if composed, all arguments in array) - // ->getByUsername('gediaz') // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' + ->getById([3, Rows::COND_SUP]) // PRIMARY KEY (if composed, all arguments in array) + ->getById([100, Rows::COND_INF]) // PRIMARY KEY (if composed, all arguments in array) + ->getByUsername(['g%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column' ->select(['mail', 'username', 'firstname']) // Select clause ->select('id_user') // Select clause (added) - ->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse' - // ->unique() // unique result + ->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse' + // ->unique() // unique result ->fetch(); // Result