Parte di codice

bismark2005

Utente Attivo
8 Mar 2011
70
0
0
Salve ragazzi, qualcuno può darmi chiarimenti sul seguente pezzo di codice?

PHP:
           } else {
                        $strValueList = "";
                        $strQuery = 'INSERT INTO "' . $this->strTableName . '"(';
                        foreach ($this->arRelationMap as $key => $value) {
                                eval('$actualVal = &$this->' . $value . ';');
                                if (isset($actualVal)) {
                                         $strQuery .= '"' . $key . '", ';
                                         $strValueList .= ":$value, ";
                                };
                        }
                        $strQuery = substr($strQuery, 0, strlen($strQuery) - 2);
                        $strValueList = substr($strValueList, 0,
                                        strlen($strValueList) - 2);
                        $strQuery .= ") VALUES (";
                        $strQuery .= $strValueList;
                        $strQuery .= ")";
                        unset($objStatement);
                        $objStatement = $this->objPDO->prepare($strQuery);
                        foreach ($this->arRelationMap as $key => $value) {
                                eval('$actualVal = &$this->' . $value . ';');
                                if (isset($actualVal)) {
                                        if ((is_int($actualVal)) || 
                                            ($actualVal == NULL)) {
                                                $objStatement->bindValue
                              (':' . $value, $actualVal, PDO::PARAM_INT);
                                        } else {
                                                $objStatement->bindValue
                              (':' . $value, $actualVal, PDO::PARAM_STR);
                                        };
                                };
                        }
                        $objStatement->execute();
                        $this->ID = $this->objPDO->lastInsertId
                                                 ($this->strTableName . "_id_seq");
                }
 
PHP:
} else {
 $strValueList = ""; 
                       //dichiara chiavi e valori dell'insert into
                        $strQuery = 'INSERT INTO "' . $this->strTableName . '"(';
                         //li cicla per differenziarli 
                        foreach ($this->arRelationMap as $key => $value) { 
                               //eval inutile $actualVal = &$this->{$value};
                                eval('$actualVal = &$this->' . $value . ';'); 
                                if (isset($actualVal)) { 
                                         $strQuery .= '"' . $key . '", '; 
                                         $strValueList .= ":$value, "; 
                                }; 
                        } 
                      //fa alcune modifiche per togliere le ultime virgole
                        $strQuery = substr($strQuery, 0, strlen($strQuery) - 2); 
                        $strValueList = substr($strValueList, 0, 
                                        strlen($strValueList) - 2); 
                        $strQuery .= ") VALUES ("; 
                        $strQuery .= $strValueList; 
                        $strQuery .= ")"; 
                       //resetta l'ultimo statement e prepara una nuova query
                        unset($objStatement); 
                        $objStatement = $this->objPDO->prepare($strQuery); 
                      //cicla tutti i parametri per renderli sicuri
                        foreach ($this->arRelationMap as $key => $value) { 
                               //eval inutile $actualVal = &$this->{$value};
                                eval('$actualVal = &$this->' . $value . ';'); 
                      //controllo che è settato il valore, controllo se è un numero intero o una stringa e li lega alla chiave
                                if (isset($actualVal)) { 
                                        if ((is_int($actualVal)) ||  
                                            ($actualVal == NULL)) { 
                                                $objStatement->bindValue 
                              (':' . $value, $actualVal, PDO::PARAM_INT); 
                                        } else { 
                                                $objStatement->bindValue 
                              (':' . $value, $actualVal, PDO::PARAM_STR); 
                                        }; 
                                }; 
                        } 
                       //esegue la query e prende l'ultimo id
                        $objStatement->execute(); 
                        $this->ID = $this->objPDO->lastInsertId 
                                                 ($this->strTableName . "_id_seq"); 
                }

La parte della trasformazione è riassumibile in pochi righi
PHP:
                        $strQuery = 'INSERT INTO "' . $this->strTableName . '"('; 
                        foreach ($this->arRelationMap as $value)  $strValueList .= ":$value, "; 
                        $strValueList = substr($strValueList, 0,  strlen($strValueList) - 2); 
                        $strQuery .= join(',',array_keys($this->arRelationMap)).") VALUES ($strValueList)";

Inoltre poteva modificare i valori direttamente nell'array di relazione e passarlo nell'execute, comunque tende a complicare le cose per me senza contare che servirebbe anche gestire eventuali errori o eccezioni tramite try-catch
 
PHP:
} else {
 $strValueList = ""; 
                       //dichiara chiavi e valori dell'insert into
                        $strQuery = 'INSERT INTO "' . $this->strTableName . '"(';
                         //li cicla per differenziarli 
                        foreach ($this->arRelationMap as $key => $value) { 
                               //eval inutile $actualVal = &$this->{$value};
                                eval('$actualVal = &$this->' . $value . ';'); 
                                if (isset($actualVal)) { 
                                         $strQuery .= '"' . $key . '", '; 
                                         $strValueList .= ":$value, "; 
                                }; 
                        } 
                      //fa alcune modifiche per togliere le ultime virgole
                        $strQuery = substr($strQuery, 0, strlen($strQuery) - 2); 
                        $strValueList = substr($strValueList, 0, 
                                        strlen($strValueList) - 2); 
                        $strQuery .= ") VALUES ("; 
                        $strQuery .= $strValueList; 
                        $strQuery .= ")"; 
                       //resetta l'ultimo statement e prepara una nuova query
                        unset($objStatement); 
                        $objStatement = $this->objPDO->prepare($strQuery); 
                      //cicla tutti i parametri per renderli sicuri
                        foreach ($this->arRelationMap as $key => $value) { 
                               //eval inutile $actualVal = &$this->{$value};
                                eval('$actualVal = &$this->' . $value . ';'); 
                      //controllo che è settato il valore, controllo se è un numero intero o una stringa e li lega alla chiave
                                if (isset($actualVal)) { 
                                        if ((is_int($actualVal)) ||  
                                            ($actualVal == NULL)) { 
                                                $objStatement->bindValue 
                              (':' . $value, $actualVal, PDO::PARAM_INT); 
                                        } else { 
                                                $objStatement->bindValue 
                              (':' . $value, $actualVal, PDO::PARAM_STR); 
                                        }; 
                                }; 
                        } 
                       //esegue la query e prende l'ultimo id
                        $objStatement->execute(); 
                        $this->ID = $this->objPDO->lastInsertId 
                                                 ($this->strTableName . "_id_seq"); 
                }

La parte della trasformazione è riassumibile in pochi righi
PHP:
                        $strQuery = 'INSERT INTO "' . $this->strTableName . '"('; 
                        foreach ($this->arRelationMap as $value)  $strValueList .= ":$value, "; 
                        $strValueList = substr($strValueList, 0,  strlen($strValueList) - 2); 
                        $strQuery .= join(',',array_keys($this->arRelationMap)).") VALUES ($strValueList)";

Inoltre poteva modificare i valori direttamente nell'array di relazione e passarlo nell'execute, comunque tende a complicare le cose per me senza contare che servirebbe anche gestire eventuali errori o eccezioni tramite try-catch

Sto meditando di abbandonare il manuale PHP 6 Guida per lo sviluppatore. Dire che è stata una delusione è poco. Codice molto prolisso nel quale mi ci perdo facilmente. Gli argomenti trattati sono ottimi, ma andrebbero "accomoagnati" con codice meno prolisso e più documentato.

Mi hanno parlato bene di questo manuale: http://www.phpbestpractices.it/

Speriamo non sia un'altra delusione.
 

Discussioni simili