Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 25
  1. #1

    Struttura corretta CRUD con logica MVC

    Ciao a tutti, sto cercando da giorni (googlando) un esempio corretto di utilizzo di MVC per gestire operazioni CRUD.
    Sapete darmi qualche dritta di dove trovare un esempio corretto e ben sviluppato, da usare come esempio didattico??

    Altro dubbio: come vedete l'uso di PDO? è la strada giusta?

    Grazie
    Iceberg

  2. #2
    Qui andiamo sulla teoria spicciola, nel senso che ci sono mille modi diversi, ognuno con vantaggi e svantaggi. Fortuna tua che con php ce ne sono di meno.

    Diciamo che i due più apprezzati sono il pattern DAO e il pattern DG, ovvero il Data Access Object e il Data Gateway. Oppure della specie di Bean che estendono VO aggiungendo metodi per la gestione del CRUD (VO = Value Object)

    Dovresti decidere te quale usare. In php, dei due framework che conosco da vicino (cakephp e zend), il primo usa una specie di Bean che offre metodi crud, il secondo và di Data Gateway ( es: http://www.longacre-scm.com/blog/ind...e-data-gateway ).

    Il problema ovviamente è generico, infatti stà tutto nella M dell'MVC, V e C se ne interessano poco di che pattern usi. Come è giusto che sia. Tendenzialmente M non è semplicemente "CRUD con Oggetti", ma è l'insieme delle logiche di business sfruttate poi da V e C. Nel senso che il pattern usato per la persistenza dei dati è nascosto al controller perchè il controller usa metodi e oggetti (in genere) di più alto livello.

    Cmq, puoi sempre inventarti il tuo metodo per gestire il CRUD. I pattern sono linee guida, nessuno ti obbliga cmq a seguirle

    Da un punto di vista personale, DAO e DG sono molto "verbosi" e tendenzialmente creano molti oggetti e interfacce (soprattutto DAO), però ti permettono di avere la logica crud separata interamente dal VO (soprattutto in DAO) e quindi usi questi oggettini stupidi stupidi che danno pochi problemi di trasporto. Il metodo Bean riduce drasticamente il numero di oggetti e interfacce da utilizzare, però accorpa la rappresentazione dei dati (VO) alla logica CRUD, e questo ha delle ripercussioni sull'uso dell'oggetto o comunque porta ad avere una classe "mista", ovvero che compie sia rappresentazione di dati che logica crud che potrebbe portare a confusione e codice poco chiaro se usata male.

    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

  3. #3
    Grazie per le risposte, quello che vorrei capire è questo.

    Ipotizzando di non voler usare alcun FMK.

    La gestione migliore delle query quale sarebbe? intendo i parametri passati per le INSERT, UPDATE....

    io a volte faccio qualche cosa del tipo

    Codice PHP:


    class LegalDAO extends BasicDAO{

        function 
    search($where$page$rp$sort) {
            
    $start = (($page-1) * $rp);

            
    $limit "LIMIT $start$rp";
            
            
    $sql "SELECT * FROM tbl_ ";
            
            
            if(
    $where != "") {
                
    $sql "$sql $where";    
            }
            
            
    $total $this->get_num_rows($sql);
        
            
            
    $sql.="$sort $limit";
            
    $data['page'] = $page;
            
    $data['total'] = $total;
            
    $rows = array();.....

            
    $data['rows'] = $rows;
            return 
    $data;
        }
        
        function 
    insert($data) {
            
            return 
    $this->DB->insert("tbl_"$data);
        }
        
        function 
    update($data$where) {
            return 
    $this->DB->update("tbl_"$data$where);
        }
        
        function 
    delete($where) {
            return 
    $this->DB->delete("tbl_"$where);
        }


    come vi sembra???

    voi che soluzione usate per gestire i parametri passate alle query?

    Grazie
    Iceberg

  4. #4
    Codice PHP:
    function build_and_exec_query$a )
        {
            
    $this->build_query$a );

            
    $ci $this->exec_query();

            if ( isset(
    $a['select']) AND $a['select'] )
            {
                return 
    $this->fetch_row$ci );
            }
        }

    function 
    exec_query()
        {
            if ( 
    $this->cur_query != "" )
            {
                
    $ci $this->query$this->cur_query );
            }

            
    $this->cur_query   "";
            
    $this->is_shutdown 0;
            return 
    $ci;
        }

    function 
    build_query$a )
        {
            if ( isset(
    $a['select']) && $a['select'] )
            {
                if ( isset(
    $a['add_join']) && is_array$a['add_join'] ) )
                {
                    
    $this->simple_select_with_join$a['select'], $a['from'], isset($a['where']) ? $a['where'] : ''$a['add_join'] );
                }
                else
                {
                    
    $this->simple_select$a['select'], $a['from'], isset($a['where']) ? $a['where'] : '' );
                }
            }

            if ( isset(
    $a['update']) && $a['update'] )
            {
                
    $this->simple_update$a['update'], $a['set'], isset($a['where']) ? $a['where'] : '', isset($a['lowpro']) ? $a['lowpro'] : '' );
            }

            if ( isset(
    $a['delete']) && $a['delete'] )
            {
                
    $this->simple_delete$a['delete'], $a['where'] );
            }

            if ( isset(
    $a['group']) && $a['group'] )
            {
                
    $this->simple_group$a['group'] );
            }

            if ( isset(
    $a['order']) && $a['order'] )
            {
                
    $this->simple_order$a['order'] );
            }

            if ( isset(
    $a['limit']) && is_array$a['limit'] ) )
            {
                
    $this->simple_limit$a['limit'][0], $a['limit'][1] );
            }
        }

    function 
    simple_select$get$table$where="" )
        {
            
    $this->cur_query .= "SELECT $get FROM ".$this->obj['sql_tbl_prefix']."$table";

            if ( 
    $where != "" )
            {
                
    $this->cur_query .= " WHERE ".$where;
            }
        }

        
    /*-------------------------------------------------------------------------*/
        // SIMPLE: SELECT WITH JOIN
        /*-------------------------------------------------------------------------*/

    function simple_select_with_join$get$table$where=""$add_join=array() )
        {
            
    //-----------------------------------------
            // OK, here we go...
            //-----------------------------------------

            
    $select_array   = array();
            
    $from_array     = array();
            
    $joinleft_array = array();
            
    $where_array    = array();
            
    $final_from     = array();

            
    $select_array[] = $get;
            
    $from_array[]   = $table;

            if ( 
    $where )
            {
                
    $where_array[]  = $where;
            }

            
    //-----------------------------------------
            // Loop through JOINs and sort info
            //-----------------------------------------

            
    if ( is_array$add_join ) and count$add_join ) )
            {
                foreach( 
    $add_join as $join )
                {
                    
    # Push join's select to stack
                    
    if ( isset($join['select']) AND $join['select'] )
                    {
                        
    $select_array[] = $join['select'];
                    }

                    if ( 
    $join['type'] == 'inner' )
                    {
                        
    # Join is inline
                        
    $from_array[]  = $join['from'];

                        if ( 
    $join['where'] )
                        {
                            
    $where_array[] = $join['where'];
                        }
                    }
                    else if ( 
    $join['type'] == 'left' )
                    {
                        
    # Join is left
                        
    $tmp " LEFT JOIN ";

                        foreach( 
    $join['from'] as $tbl => $alias )
                        {
                            
    $tmp .= $this->obj['sql_tbl_prefix'].$tbl.' '.$alias;
                        }

                        if ( 
    $join['where'] )
                        {
                            
    $tmp .= " ON ( ".$join['where']." ) ";
                        }

                        
    $joinleft_array[] = $tmp;

                        unset( 
    $tmp );
                    }
                    else
                    {
                        
    # Not using any other type of join
                    
    }
                }
            }

            
    //-----------------------------------------
            // Build it..
            //-----------------------------------------

            
    foreach( $from_array as $i )
            {
                foreach( 
    $i as $tbl => $alias )
                {
                    
    $final_from[] = $this->obj['sql_tbl_prefix'].$tbl.' '.$alias;
                }
            }

            
    $get   implode","     $select_array   );
            
    $table implode","     $final_from     );
            
    $where implode" AND " $where_array    );
            
    $join  implode"\n"    $joinleft_array );

            
    $this->cur_query .= "SELECT $get FROM $table";

            if ( 
    $join )
            {
                
    $this->cur_query .= " ".$join." ";
            }

            if ( 
    $where != "" )
            {
                
    $this->cur_query .= " WHERE ".$where;
            }
        }

    ecc 

  5. #5
    Questa sarebbe quello che usi nel tuo DAO??

    per la gestione delle query??
    Iceberg

  6. #6
    Questa è una parte di una classe che gestisce le query parametrizzate

  7. #7
    La versione completa del file posso trovarla on line.

    Vorrei chiederti anche questo:

    cosa ne pensi di questa soluzione

    http://www.phpdao.com/

    l'ho trovata oggi e genera già le classi DAO ecc..


    come vedi l'utilizzo di questa base?

    Grazie per tutti i suggerimenti
    Iceberg

  8. #8
    Se la prima è una domanda allora la risposta è "ni".
    Ho preso di base una classe già fatta e poi l'ho manipolata per le mie esigenze.

    Riguardo il generatore che indichi può valere la pena ma anche no.
    Dipende dal tuo scopo finale.

    Io preferisco sempre parametrizzare al massimo e quel generatore non lo fa...anzi complica le cose dato che genera una erie di classi e interfacce per ogni singola tabella del Db..

  9. #9
    Cosa intendi con "parametrizzare" al massimo??

    intendi proprio passare parametri alle query??
    Iceberg

  10. #10
    Intendo usare un metodo che accetta dei parametri e costruire la query (qualunque) in base ai parametri, proprio come nell'esempio che ti ho postato.
    Un unico metodo per tutte le query.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.