Ver Mensaje Individual
  #7 (permalink)  
Antiguo 11/05/2004, 12:58
Herminio Heredia San
 
Fecha de Ingreso: septiembre-2001
Ubicación: Monterrey
Mensajes: 663
Antigüedad: 22 años, 7 meses
Puntos: 6
Clase Noticias:
Código PHP:
class Noticias{
    
        var 
ArrayPropiedades null;
    
        function 
Noticias$objDatos null){
        
            if( 
$objDatos !== null && is_array$objDatos ) ){
            
                foreach( 
$objDatos as $k $v ){
                
                    
$this->ArrayPropiedades[$k] = $v;
                
                }
            
            }
            return;
        
        }
        
        function 
obtId(){
        
            switch( 
func_num_args()){
            
                case 
0:
                        return 
$this->ArrayPropiedades['id_noticia'];
                        break;
                case 
1:
                        
$this->ArrayPropiedades['id_noticia']= func_get_arg(0);
                        break;
            
            }
        
        }
        
        function 
obtUsuarioId(){
        
            switch( 
func_num_args()){
            
                case 
0:
                        return 
$this->ArrayPropiedades['id_usuario'];
                        break;
                case 
1:
                        
$this->ArrayPropiedades['id_usuario']= func_get_arg(0);
                        break;
            
            }
        
        }
        
        function 
obtTitulo(){
        
            switch( 
func_num_args()){
            
                case 
0:
                        return 
$this->ArrayPropiedades['titulo'];
                        break;
                case 
1:
                        
$this->ArrayPropiedades['titulo']= func_get_arg(0);
                        break;
            
            }
        
        }
        
        function 
obtDescripcion(){
        
            switch( 
func_num_args()){
            
                case 
0:
                        return 
$this->ArrayPropiedades['descripcion'];
                        break;
                case 
1:
                        
$this->ArrayPropiedades['descripcion']= func_get_arg(0);
                        break;
            
            }
        
        }
        
        function 
obtFecha(){
        
            switch( 
func_num_args()){
            
                case 
0:
                        return 
$this->ArrayPropiedades['fecha'];
                        break;
                case 
1:
                        
$this->ArrayPropiedades['fecha']= func_get_arg(0);
                        break;
            
            }
        
        }
        
        function 
obtImagen(){
        
            switch( 
func_num_args()){
            
                case 
0:
                        return 
$this->ArrayPropiedades['imagen'];
                        break;
                case 
1:
                        
$this->ArrayPropiedades['imagen']= func_get_arg(0);
                        break;
            
            }
        
        }
        
        function 
obtLink(){
        
            switch( 
func_num_args()){
            
                case 
0:
                        return 
$this->ArrayPropiedades['link'];
                        break;
                case 
1:
                        
$this->ArrayPropiedades['link']= func_get_arg(0);
                        break;
            
            }
        
        }
    
    } 
Clase NotciasMapper:
Código PHP:
class NoticiaMapper{
    
        var 
$_bdcon null;
        
        var 
$_arrayNoticias null;
    
        function 
NoticiaMapper(){
        
            
$this->_bdcon DB::connect($dsn);
            if( 
DB::isError($this->_bdcon) )
            {
                die( 
$this->_bdcon->getMessage( ) );
            }
        
        }
        
        function 
obtListaNoticias(){
        
            
$sql 'SELECT * FROM noticias';
            
$sqlResult $this->_bdcon->query($sql);
            
            if( 
$this->_bdcon->isError($sqlResult)){
            
                die(
$sqlResult->getMessage());
            
            }
            
            if( 
$sqlResult->numRows() > 0){
            
                while( 
$registro $sqlResult->fetchRow(DB_FETCHMODE_ASSOC) ){
                
                    
$this->_arrayNoticias[ ] = &new Noticias$registro );
                
                }
                
                return 
$this->_arrayNoticias;
            
            }
            
            return 
false;
        
        }
        
        function 
obtNoticiaPorId$id ){
        
            
$strSql 'SELECT * FROM noticias WHERE id_noticia = %d';
            
$sql sprintf$strSql, (int)$id );
            
$this->_bdcon->query($sql);
            
            if( 
$this->_bdcon->isError($sqlResult)){
            
                die(
$sqlResult->getMessage());
            
            }
            
            if( 
$sqlResult->numRows() > 0){
            
                while( 
$registro $sqlResult->fetchRow(DB_FETCHMODE_ASSOC) ){
                
                    
$this->_arrayNoticias[ ] = &new Noticias$registro );
                
                }
                
                return 
$this->_arrayNoticias;
            
            }
            
            return 
false;
        
        }
        
        function 
obtNoticiaPorFecha$fecha ){
        
            
$strSql 'SELECT * FROM noticias WHERE fecha = %s';
            
$sql sprintf$strSql$fecha );
            
$this->_bdcon->query($sql);
            
            if( 
$this->_bdcon->isError($sqlResult)){
            
                die(
$sqlResult->getMessage());
            
            }
            
            if( 
$sqlResult->numRows() > 0){
            
                while( 
$registro $sqlResult->fetchRow(DB_FETCHMODE_ASSOC) ){
                
                    
$this->_arrayNoticias[ ] = &new Noticias$registro );
                
                }
                
                return 
$this->_arrayNoticias;
            
            }
            
            return 
false;
        
        }
        
        function 
agregar( &$noticia ){
        
            
$strSql "INSERT INTO noticias VALUES(%d,%d,'%s')";
            
$propObjeto = array(    
                                    
$noticia->obtTitulo(),
                                    
$noticia->obtDescripcion(),
                                    
$noticia->obtFecha(),
                                    
$noticia->obtImagen(),
                                    
$noticia->obtLink()
                                );
            
$sql sprintf(        
                                
$sql,
                                
'',
                                
$noticia->obtUsuarioId(),
                                
implode(',',$propObjeto)
                          );
                                 
            
$this->_bdcon->query($sql);
            if( 
$this->_bdcon->isError($sqlResult)){
            
                die(
$sqlResult->getMessage());
            
            }
            
            if(
$this->_bdcon->affectedRows()){
            
                return 
true;
            
            }else{
            
                return 
false;
            
            }
        }
        
        function 
actualizar( &$noticia ){
        
            
$strSql "UPDATE noticias SET id_usuarios = %d,
                                            titulo = '%s',
                                            descripcion = '%s',
                                            fecha = '%s',
                                            imagen = '%s',
                                            link = '%s'"
;
    
            
$sql sprintf(        
                                
$sql,
                                
$noticia->obtUsuarioId(),
                                
$noticia->obtTitulo(),
                                
$noticia->obtDescripcion(),
                                
$noticia->obtFecha(),
                                
$noticia->obtImagen(),
                                
$noticia->obtLink(),
                          );
                                 
            
$this->_bdcon->query($sql);
            if( 
$this->_bdcon->isError($sqlResult)){
            
                die(
$sqlResult->getMessage());
            
            }
            
            if(
$this->_bdcon->affectedRows()){
            
                return 
true;
            
            }else{
            
                return 
false;
            
            }
        }
        
        function 
actualizar( &$noticia ){
        
            
$strSql "DELETE FROM noticias WHERE id_noticia = %d";
    
            
$sql sprintf(        
                                
$sql,
                                
$noticia->obtId(),
                          );
                                 
            
$this->_bdcon->query($sql);
            if( 
$this->_bdcon->isError($sqlResult)){
            
                die(
$sqlResult->getMessage());
            
            }
            
            if(
$this->_bdcon->affectedRows()){
            
                return 
true;
            
            }else{
            
                return 
false;
            
            }
        }
    
    } 
Para la implementacion del la clase NotciasMapper hago uso de una clase de abstracion para acceso a base de datos que viene en las pear y cuya documentacion puede ver en http://pear.php.net/manual/en/packag...ge.database.db pero puedes adecuar la clase para usar alguna otra clase de abstraccion. Las clases no estan documentadas por que las acabo de hacer hace un momento. Pero no creo que esten muy complejas.
Por ciertos los diagrmas esta hecho con la version free del poseidon. Por si notan algunos dibujos en el fondo de las imagenes.
Bien espero que esto te sriva de ayuda y a los mejor se complementa con la opinion de los demas foreros que andan rondando por aqui.. Ya que esto que te comento es solo una perspectiva a lo mejor puedes haber mejores opciones o el uso de otros patrones como los patrones table data gateway , row data gateway , etc...
Recuerda probarlas clases estan escritas al vuelo.

Última edición por Herminio Heredia San; 11/05/2004 a las 15:03