Originariamente inviato da Fractals87
Ok è abbastanza chiaro... questa tua struttura gestisce in todo le cosidette operazioni crud..
Ma per generare l'html come faresti?
Intedi creare un metodo taxcodeModel che ti restituisce ad esempio la forma tabellare del tuo oggetto Es :
taxcode.class.php
Codice PHP:
public GetTabletaxcode(){
$html ="<table>";
$html.="<td>id</td>";
$html.="<td>uid</td>";
$html.="<td>code</td>";
$html.="<td>description</td>";
foreach (getTaxcodebycode as $taxcode){
$html.="<td>".$taxcode['id']."</td>";
$html.="<td>".$taxcode['uid']."</td>";
$html.="<td>".$taxcode['code']."</td>";
$html.="<td>".$taxcode['description']."</td>";
}
$html.="</table>";
}
taxcode.php
Codice PHP:
<?php
$taxcodeModel = taxcodeModel($PDO);
$taxcodeModel-->GetTabletaxcode();
?>
Supponendo di non aver scritto una grandissima cazzata, come gestiresti la visualizzazione della singola taxcode in una form di modifica?
fermo.. il modello fornisce i dati, la vista li mostra al dispositivo.
i dati di cui avrai bisogno, a parte qualche rara eccezzione particolare sono:
1 campo
1 riga
N righe
1 campo io me lo faccio restituire dalla classe madre astratta
Codice PHP:
public function __get( $name ) {
if ( !in_array( $name, $this->fields ) ) {
throw new entityException( 'The field [ ' . $name . ' ] is not allowed "get" for this entity.' );
}
$accessor = 'get' . ucfirst( $name );
if ( method_exists( $this, $accessor ) && is_callable( array( $this, $accessor ) ) ) {
return $this->$accessor();
} else {
if ( $this->id ) {
$selectQuery = "SELECT $name FROM $this->table WHERE id = :id";
$sql = $this->pdo->prepare( $selectQuery );
if ( $sql->execute( array( ':id'=>$this->id ) ) ) {
return $sql->fetchColumn();
}
} else {
// var_dump( $this );
throw new entityException( __METHOD__ . "( $name ), You must set an ID for the selection." );
}
}
}
se non trovo un "accessor" (quindi se non esiste un metodo particolare da richiamare per selezionare un determinato campo) effettuo la selezione direttamente nel db.
Quindi ad esempio puoi benissimo fare:
Codice PHP:
$taxcodeModel = new taxcodeModel( $PDO );
$taxcodeModel->id = "123456";
echo $taxcodeModel->code;
echo $taxcodeModel->description;
gli altri 2 valori (la riga e le righe) le trovi sulla classe estesa.
Codice PHP:
public function getTaxcode() {
$selectQuery = "SELECT * FROM " . $this->table . " WHERE id = :id LIMIT 0,1";
if ( $this->id ) {
$sql = $this->pdo->prepare( $selectQuery );
if ( $sql->execute( array( ':id'=>$this->id ) ) ) {
return $sql->fetch( PDO::FETCH_OBJ );
}
} else {
$this->msg = "Devi impostare un ID di selezione.";
}
}
public function getTaxcodes() {
$selectQuery = "SELECT * FROM " . $this->table;
if ( $this->options != '' ) {
$selectQuery .= ' ' . $this->options;
}
$sql = $this->pdo->prepare( $selectQuery );
if ( $sql->execute( $input ) ) {
$rows = array();
while ( $row = $sql->fetch( PDO::FETCH_OBJ ) ) {
$rows[] = $row;
}
return $rows;
}
}
io lavoro con gli oggetti quindi PDO::FETCH_OBJ mi restituisce la riga o le righe sotto forma di oggetto...
Codice PHP:
$taxcodeModel = new taxcodeModel( $PDO );
$taxcodeModel->id = "123456";
var_dump( $taxcodeModel->taxcode );
questi valori nudi e crudi verranno poi elaborati con il controller e mostrati/iniettati dalla vista.. o tramite decorator o altro a piacimento.