Fetch des relations done

This commit is contained in:
xdrm-brackets 2016-11-05 19:08:14 +01:00
parent 26f974ef1e
commit 7626adf3d5
2 changed files with 74 additions and 26 deletions

View File

@ -188,16 +188,17 @@
$column_name = '';
/* (1) formatte la requête 'MyAttribute' -> 'my_attribute' */
for( $l = 0 ; $l < strlen($regex[1]) ; $l++ ){
for( $l = 0, $ll = strlen($regex[1]) ; $l < $ll ; $l++ ){
$letter = $regex[1][$l];
// Si la lettre est en majuscule mais que c'est pas la première
// Si la lettre est en majuscule mais que c'est pas la première ni un seul mot
if( strtoupper($letter) == $letter && $l > 0 )
$column_name .= '_';
$column_name .= strtolower($letter);
}
/* (2) On vérifie que la colonne existe */
if( !isset($this->schema['columns'][$column_name]) )
return $this; // si n'existe pas, on ne fait rien
@ -205,44 +206,86 @@
/* [2] On vérifie le type du paramètre
=========================================================*/
/* (1) Si aucun param, on quitte */
if( count($a) == 0 )
// On délègue
$args = array_merge([$column_name], $a);
return call_user_func_array([$this, 'where'], $args);
}
public function where($field){
// get arguments
$args = array_slice(func_get_args(), 1);
/* [1] Vérification de l'argument @field
=========================================================*/
/* (1) Type de @field */
if( !is_string($field) )
return $this;
/* (2) Si c'est un paramètre seul, on ajoute par défaut self::COND_EQUAL */
if( !is_array($a[0]) )
$a[0] = [ $a[0], self::COND_EQUAL ];
/* (2) On vérifie que la colonne existe */
if( !isset($this->schema['columns'][$field]) )
return $this; // si n'existe pas, on ne fait rien
/* (3) Si type INT et pas numérique */
if( $this->schema['columns'][$column_name]['type'] == 'int' && !is_numeric($a[0][0]) )
/* [2] On vérifie le type du paramètre
=========================================================*/
/* (1) Si au moins 1 param */
if( count($args) < 1 )
return $this;
/* (4) Si type FLOAT et pas numérique */
if( $this->schema['columns'][$column_name]['type'] == 'float' && !is_numeric($a[0][0]) )
/* [3] If `IN` condition
=========================================================*/
$defaultWhere = $this->where;
$inCond = count($args[0]) > 1 && is_array($args[0][0]) && $args[0][1] == self::COND_IN;
// erreur
if( is_array($args[0][0]) && !$inCond )
return $this;
/* (5) Si type STRING et pas string */
if( $this->schema['columns'][$column_name]['type'] == 'text' && !is_string($a[0][0]) )
return $this;
/* (1) Si c'est une condition "IN"
---------------------------------------------------------*/
if( $inCond ){
/* (1) On vérifie le type de chaque valeur du IN */
$type = $this->schema['columns'][$field]['type'];
foreach($args[0][0] as $value){
if( $type == 'int' && !is_numeric($value) ) return $this;
if( $type == 'float' && !is_numeric($value) ) return $this;
if( in_array($type, ['text', 'varchar']) && !is_string($value) ) return $this;
}
/* (2) Si c'est une condition "simple"
---------------------------------------------------------*/
}else{
/* (1) Si le type de condition est manquant, on met EQUAL par défaut */
if( !is_array($args[0]) )
$args[0] = [ $args[0], self::COND_EQUAL ];
/* (2) On vérifie le type de chaque valeur */
$type = $this->schema['columns'][$field]['type'];
if( $type == 'int' && !is_numeric($args[0][0]) ) return $this;
if( $type == 'float' && !is_numeric($args[0][0]) ) return $this;
if( in_array($type, ['text', 'varchar']) && !is_string($args[0][0]) ) return $this;
}
/* [3] Si type OK, on enregistre la condition
=========================================================*/
/* (1) Si aucune condition pour ce champ, on crée un tableau */
if( !isset($this->where[$column_name]) )
$this->where[$column_name] = [];
if( !isset($this->where[$field]) )
$this->where[$field] = [];
/* (2) On ajoute la condition */
$this->where[$column_name][] = $a[0];
$this->where[$field][] = $args[0];
// On retourne l'object courant
return $this;
}
/* SELECTIONNE UNIQUEMENT LE CHAMP SELECTIONNE
*
* @field<String> Libellé du champ à afficher
@ -844,7 +887,6 @@
// On ajoute la clause FROM de jointure à la clause FROM locale //
$requestS['FROM'] = array_merge($data['request']['FROM'], $requestS['FROM']);
/* [5] On rédige la clause WHERE/AND
=========================================================*/
/* (1) On met les conditions locales */
@ -946,7 +988,6 @@
/* (3) On prépare la requête */
$request = DatabaseDriver::getPDO($this->driver)->prepare($requestString);
// var_dump($requestString);
/* [8] On exécute la requête et retourne le résultat

View File

@ -138,12 +138,15 @@
->unique();
$A = Table::get('sujets', 'lab-surveys')
->select('pseudo', null, null, 'A')
->select('idSujet')
->join('idEtude', $lisst)
->orderby('idSujet');
->orderby('idSujet')
->fetch();
$Ain = [];
foreach($A as $i=>$v)
$Ain[] = $v['idSujet'];
$B = Table::get('sujets', 'lab-surveys')
->select('pseudo', null, null, 'B')
->join('idEtude', $lisst)
->orderby('idSujet');
@ -151,7 +154,11 @@
->select('intitule', null, null, 'categorie');
$rel = Table::get('relations', 'lab-surveys')
->join('idSujetA', $A)
->select('idSujetA', Rows::SEL_CONCAT)
->select('idSujetB')
->select('knows')
->select('isEgo')
->where('idSujetA', [$Ain, Rows::COND_IN])
->join('idSujetB', $B)
->join('idCategorieRelation', $cate);