|
Re: Criteria is not null |
|
Guest_
|
Bonjour c pas tout à fait ce que je voulais faire. Cette table est en relation avec la table user par uid. là rien de compliqué. J'ai fait cette table pour pouvoir rentrer plusieurs coordonnées par user. comme il est prévu qu'ils puissent rentrer plusieurs tel, fax et autres chose je voulais pourvoir gérer les affichages par catégorie.
Dans transMedia j'ai donc uid=id user et pour lister que les fax j'avais besoin de faire une requête select uid=idUser et fax non-null pour ne pas remonter par exemple un mail ou tel mais que les fax.
pour le moment la solution qui marche mais qui est loin d'être belle est d'avoir rajouté dans la base des booleen pour faire que la requete soit select uid=UserId and BooolFax=1 puisque les class ne peuvent le faire.
Je suis donc toujours preneur d'une solution pour faire passer la requete par criteria gérant le "notnull"
Merci pour cette lecture
Cdl William
Posté le : 26/08/2006 13:20
|
|
|
Re: Criteria is not null |
|
Semi pro 
Inscrit: 06/01/2004 09:37
De Non loin de Paris
|
Est ce que tu peut poster la structure de ta table, ca facilitera les chose 
Posté le : 26/08/2006 14:01
|
|
|
Re: Criteria is not null |
|
Guest_
|
Bonsoir Oui pas de souci la voici
idTransMedia MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
date DATE NULL,
uid MEDIUMINT(8) UNSIGNED NULL,
mail VARCHAR(50) NULL,
msn VARCHAR(50) NULL,
skype VARCHAR(50) NULL,
yahoo VARCHAR(50) NULL,
gsm VARCHAR(20) NULL,
tel VARCHAR(20) NULL,
site VARCHAR(50) NULL,
fax VARCHAR(20) NULL,
bFax BOOL NULL,
bMail BOOL NULL,
bUrl BOOL NULL,
bMsn BOOL NULL,
bSkype BOOL NULL,
bTel BOOL NULL,
bYahoo BOOL NULL,
PRIMARY KEY(idTransMedia),
INDEX uid(uid)
tous les bool à la base n'y sont pas. ils sont là puisqu'on ne peut pas gérer les non null avec la class, mais comment faire autrement sans le "non null"? Bonne nuit Cdl William Ps ce qui ne me rassure pas c'est que j'ai pleins de tables et devoir mettre des bool rien que pour vérifier la présence alors que le rec fait présence lui meme C vraiment C..
Posté le : 26/08/2006 21:43
|
|
|
Re: Criteria is not null |
|
Régulier 
Inscrit: 06/01/2006 23:55
|
Essayes de pas rajouter de booleen... ca rime a rien en terme logique de structure de base et tu doublonnes enfait le fonctionnement de ta base de donnée ;) Typiquement une donnée nulle en base c'est gere par un bit indiquant si la donnée est renseignée ou pas, donc un flag ;) sauf que 1 bit pour une colonne, ca veut dire que tu geres plusieurs colonnes avec un seul octet...
Tu peux faire ton test en utilisant la meme solution que celle que j'ai utilisé moi en utilisant la fonction ifnull (cf le sujet que je t'ai mis en lien au dessus)
Le probléme qu'il y a avec la solution que j'ai utilisé et qui devrait marcher pour toi aussi, c'est que je considere pour moi, que NULL ou 0 c'est pareil. Mais des fois fonctionnellement c'est pas du tout le cas!!! Et on peut pas le faire dans l'etat actuel des choses dans xoops avec la classe criteria!!!! Et c'est assez grave a mon avis...
Posté le : 27/08/2006 00:22
|
|
|
Re: Criteria is not null |
|
Guest_
|
Bonjour Oui je suis bien d'accord. C bool ne servent à rine. je vais voir si je ne peux pas mofidier ma base pour mettre une valeur par défaut à zéro ce qui devrait me permettre d'utiliser ta fonction ifnull
on peut toujours attendre une nouvelle écriture de la class. cdl William
Posté le : 27/08/2006 10:25
|
|
|
Re: Criteria is not null |
|
Semi pro 
Inscrit: 06/01/2004 09:37
De Non loin de Paris
|
Pourquoi tu ne déclare pas ton champ comme : fax VARCHAR(20) NOT NULL, Comme ca tu n'a qu'a faire une requette du genre : WHERE fax != '' Et ca ca passe trés bien avec la classe criteria. Un numéro de fax NULL ou qui ne contient aucune donnée c'est pareil 
Posté le : 27/08/2006 13:23
|
|
|
Re: Criteria is not null |
|
Régulier 
Inscrit: 06/01/2006 23:55
|
ca revient au meme que l'utilisation de la fonction ifnull...
C un petit bricolage qui fonctionne pour ce cas precis comme pour moi avec 0 mais fonctionnellement c'est pas toujours le cas... Imagines que tu geres un compte d'un client... un compte a null ca peut vouloir dire un compte qui n'a jamais eu aucune operation, un compte a 0 c'est pas pareil ...
Null ca veut bien dire quelquechose, c'est pas toujours pareil que 0 ou qu'une chaine vide. Et c'est bien dommage que les requetes de base IS null ou IS NOT null ne soient pas possibles avec la classe criteria tel qu'elle est aujourd'hui.
Posté le : 27/08/2006 20:37
|
|
|
Re: Criteria is not null |
|
Régulier 
Inscrit: 06/01/2006 23:55
|
Ca marche chez moi de cette façon. Pour moi IS NULL ou IS NOT NULL sont des operateurs donc j'ai modifié la classe criteria pour que cela soit pris comme tel (je mets tout le fichier criteria et pas que la methode render car j'ai modifie un truc aussi ailleurs)
<?php
// $Id: criteria.php 558 2006-06-20 06:35:23Z skalpa $
// ------------------------------------------------------------------------ //
// XOOPS - PHP Content Management System //
// Copyright (c) 2000 XOOPS.org //
// <http://www.xoops.org/> ... nbsp; //
// ------------------------------------------------------------------------ //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// You may not change or alter any portion of this comment or credits //
// of supporting developers from this source code or any supporting //
// source code which is considered copyrighted (c) material of the //
// original comment or credit authors. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
// ------------------------------------------------------------------------ //
// Author: Kazumi Ono (AKA onokazu) //
// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
// Project: The XOOPS Project //
// ------------------------------------------------------------------------- //
// Modified by: Nathan Dial //
// Date: 20 March 2003 //
// Desc: added experimental LDAP filter generation code //
// also refactored to remove about 20 lines of redundant code. //
// ------------------------------------------------------------------------- //
// Modified by: Lionel FORTE Aka Garrath //
// Date: 28 Aout 2006 //
// Desc: added IS NULL and IS NOT NULL test //
// ------------------------------------------------------------------------- //
/**
*
*
* @package kernel
* @subpackage database
*
* @author Kazumi Ono <onokazu@xoops.org>
* @copyright copyright (c) 2000-2003 XOOPS.org
*/
/**
* A criteria (grammar?) for a database query.
*
* Abstract base class should never be instantiated directly.
*
* @abstract
*
* @package kernel
* @subpackage database
*
* @author Kazumi Ono <onokazu@xoops.org>
* @copyright copyright (c) 2000-2003 XOOPS.org
*/
class CriteriaElement
{
/**
* Sort order
* @var string
*/
var $order = 'ASC';
/**
* @var string
*/
var $sort = '';
/**
* Number of records to retrieve
* @var int
*/
var $limit = 0;
/**
* Offset of first record
* @var int
*/
var $start = 0;
/**
* @var string
*/
var $groupby = '';
/**
* Constructor
**/
function CriteriaElement()
{
}
/**
* Render the criteria element
*/
function render()
{
}
/**#@+
* Accessor
*/
/**
* @param string $sort
*/
function setSort($sort)
{
$this->sort = $sort;
}
/**
* @return string
*/
function getSort()
{
return $this->sort;
}
/**
* @param string $order
*/
function setOrder($order)
{
if ('DESC' == strtoupper($order)) {
$this->order = 'DESC';
}
}
/**
* @return string
*/
function getOrder()
{
return $this->order;
}
/**
* @param int $limit
*/
function setLimit($limit=0)
{
$this->limit = intval($limit);
}
/**
* @return int
*/
function getLimit()
{
return $this->limit;
}
/**
* @param int $start
*/
function setStart($start=0)
{
$this->start = intval($start);
}
/**
* @return int
*/
function getStart()
{
return $this->start;
}
/**
* @param string $group
*/
function setGroupby($group){
$this->groupby = $group;
}
/**
* @return string
*/
function getGroupby(){
return ' GROUP BY '.$this->groupby;
}
/**#@-*/
}
/**
* Collection of multiple {@link CriteriaElement}s
*
* @package kernel
* @subpackage database
*
* @author Kazumi Ono <onokazu@xoops.org>
* @copyright copyright (c) 2000-2003 XOOPS.org
*/
class CriteriaCompo extends CriteriaElement
{
/**
* The elements of the collection
* @var array Array of {@link CriteriaElement} objects
*/
var $criteriaElements = array();
/**
* Conditions
* @var array
*/
var $conditions = array();
/**
* Constructor
*
* @param object $ele
* @param string $condition
**/
function CriteriaCompo($ele=null, $condition='AND')
{
if (isset($ele) && is_object($ele)) {
$this->add($ele, $condition);
}
}
/**
* Add an element
*
* @param object &$criteriaElement
* @param string $condition
*
* @return object reference to this collection
**/
function &add(&$criteriaElement, $condition='AND')
{
$this->criteriaElements[] =& $criteriaElement;
$this->conditions[] = $condition;
return $this;
}
/**
* Make the criteria into a query string
*
* @return string
*/
function render()
{
$ret = '';
$count = count($this->criteriaElements);
if ($count > 0) {
$ret = '('. $this->criteriaElements[0]->render();
for ($i = 1; $i < $count; $i++) {
// Garrath 28/08/2006
// Correction au cas ou criteriaElements[$i]->render()
// ne ramene rien pas la peine de mettre le AND ou OR
$critere = $this->criteriaElements[$i]->render();
if ($critere)
$ret .= ' '.$this->conditions[$i].' '.$critere;
}
$ret .= ')';
}
return $ret;
}
/**
* Make the criteria into a SQL "WHERE" clause
*
* @return string
*/
function renderWhere()
{
$ret = $this->render();
$ret = ($ret != '') ? 'WHERE ' . $ret : $ret;
return $ret;
}
/**
* Generate an LDAP filter from criteria
*
* @return string
* @author Nathan Dial ndial@trillion21.com
*/
function renderLdap(){
$retval = '';
$count = count($this->criteriaElements);
if ($count > 0) {
$retval = $this->criteriaElements[0]->renderLdap();
for ($i = 1; $i < $count; $i++) {
$cond = $this->conditions[$i];
if(strtoupper($cond) == 'AND'){
$op = '&';
} elseif (strtoupper($cond)=='OR'){
$op = '|';
}
$retval = "($op$retval" . $this->criteriaElements[$i]->renderLdap().")";
}
}
return $retval;
}
}
/**
* A single criteria
*
* @package kernel
* @subpackage database
*
* @author Kazumi Ono <onokazu@xoops.org>
* @copyright copyright (c) 2000-2003 XOOPS.org
*/
class Criteria extends CriteriaElement
{
/**
* @var string
*/
var $prefix;
var $function;
var $column;
var $operator;
var $value;
/**
* Constructor
*
* @param string $column
* @param string $value
* @param string $operator
**/
function Criteria($column, $value='', $operator='=', $prefix = '', $function = '') {
$this->prefix = $prefix;
$this->function = $function;
$this->column = $column;
$this->value = $value;
$this->operator = $operator;
}
/**
* Make a sql condition string
*
* @return string
**/
function render() {
// Garrath 28/08/2006
// Rajout test sur IS NULL ou IS NOT NULL
if ( in_array( strtoupper($this->operator), array('IS NULL', 'IS NOT NULL')))
{
$value = '';
}
elseif ('' === ($value = trim($this->value))){
return '';
}
elseif ( !in_array( strtoupper($this->operator), array('IN', 'NOT IN') ) ) {
if ( ( substr( $value, 0, 1 ) != '`' ) && ( substr( $value, -1 ) != '`' ) ) {
$value = "'$value'";
} elseif ( !preg_match( '/^[a-zA-Z0-9_.-`]*$/', $value ) ) {
$value = '``';
}
}
$clause = (!empty($this->prefix) ? "{$this->prefix}." : "") . $this->column;
if ( !empty($this->function) ) {
$clause = sprintf($this->function, $clause);
}
$clause .= " {$this->operator} $value";
return $clause;
}
/**
* Generate an LDAP filter from criteria
*
* @return string
* @author Nathan Dial ndial@trillion21.com, improved by Pierre-Eric MENUET pemen@sourceforge.net
*/
function renderLdap(){
if ($this->operator == '>') {
$this->operator = '>=';
}
if ($this->operator == '<') {
$this->operator = '<=';
}
if ($this->operator == '!=' || $this->operator == '<>') {
$operator = '=';
$clause = "(!(" . $this->column . $operator . $this->value . "))";
}
else {
if ($this->operator == 'IN') {
$newvalue = str_replace(array('(',')'),'',
$this->value);
$tab = explode(',',$newvalue);
foreach ($tab as $uid)
{
$clause .= '(' . $this->column . '=' . $uid
.')';
}
$clause = '(|' . $clause . ')';
}
else {
$clause = "(" . $this->column . $this->operator . $this->value . ")";
}
}
return $clause;
}
/**
* Make a SQL "WHERE" clause
*
* @return string
*/
function renderWhere() {
$cond = $this->render();
return empty($cond) ? '' : "WHERE $cond";
}
}
?>
a utiliser comme cela :
$criteria->add (new criteria('fax', '', 'is not null'));
Posté le : 28/08/2006 22:29
|
|
|
Re: Criteria is not null |
|
Guest_
|
Bonjour j'ai pas encore fait le test, mais bravo pour ce travail. y a plus qu'à faire remonter sur la communauté Xoops.
Très cordialement William
Posté le : 29/08/2006 10:05
|
|
Vous pouvez voir les sujets.
Vous ne pouvez pas débuter de nouveaux sujets.
Vous ne pouvez pas répondre aux contributions.
Vous ne pouvez pas éditer vos contributions.
Vous ne pouvez pas effacez vos contributions.
Vous ne pouvez pas ajouter de nouveaux sondages.
Vous ne pouvez pas voter en sondage.
Vous ne pouvez pas attacher des fichiers à vos contributions.
Vous ne pouvez pas poster sans approbation.
|