Originariamente inviato da Fractals87
Oddio adesso che ci penso.... a meno che tu non intenda impostare il metodo getTaxcode
già in modo opportuno per avere già il tuo oggetto significativo (intendo con tutti i campi compresi quelli provenienti da altre tabelle.)
.....
bravissimo hai capito giusto.. 
uno spezzone di un'altra classe che però rapresenta i "partners" di un'azienda:
Codice PHP:
class partnerModel extends entity {
protected $table = 'partners';
protected $fields = array( 'id', 'uid', 'name', 'address', 'country', 'town', 'district', 'cap',
'phone', 'fax', 'taxcode', 'iso', 'vat', 'pricelist', 'accounttype', 'account', 'partner',
'partnerbyvat', 'partners', 'options', 'encoder', 'msg' );
public function getPartner() {
$selectQuery = <<<QUERY
SELECT
P.*,
CO.name AS countryname,
PAY.description AS paymentdescription,
PL.description AS pricelistdescription
FROM
$this->table P
LEFT JOIN
countries CO
ON P.country = CO.id
LEFT JOIN
payments PAY
ON P.payment = PAY.id
LEFT JOIN
pricelists PL
ON P.pricelist = PL.id
WHERE
P.id = :id
LIMIT
0,1
QUERY;
if ( $this->id ) {
$sql = $this->pdo->prepare( $selectQuery );
if ( $sql->execute( array( ':id'=>$this->id ) ) ) {
if ( $this->encoder ) {
$row = $sql->fetch( PDO::FETCH_ASSOC );
return $this->encoder->setData( $row )->encode();
} else {
return $sql->fetch( PDO::FETCH_OBJ );
}
}
} else {
$this->msg = "Devi impostare un ID per la selezione.";
}
}
come puoi notare la query effettua già le join necessarie per recuperare i dati che mi servono
Perchè devi passarlo ad un controllore?
il controllore serve per verificare i dati in "input" se necessario..
tornando alla classe taxcode, il metodo per l'inserimento di un nuovo taxcode passando dal controller è:
Codice PHP:
public function add() {
if ( isset( $_POST['taxcode-add-form-submit'] ) ) {
$fv = new formValidator();
$fv->validateEmpty( 'code', 'You must insert a value for the field [code]' );
$fv->validateEmpty( 'description', 'You must insert a value for the field [description]' );
$fv->validateEmpty( 'amount', 'You must insert a value for the field [amount]' );
$fv->validateEmpty( 'fulldescription', 'You must insert a value for the field [fulldescription]' );
( !empty( $_POST['code'] ) ) ? $fv->validateRange( 'code', 'The field [code] has a max length of 32', 32 ) : NULL;
( !empty( $_POST['description'] ) ) ? $fv->validateRange( 'description', 'The field [description] has a max length of 32', 32 ) : NULL;
( !empty( $_POST['amount'] ) ) ? $fv->validateNumber( 'amount', 'The field [amount] is not a number.' ) : NULL;
( !empty( $_POST['fulldescription'] ) ) ? $fv->validateRange( 'fulldescription', 'The field [fulldescription] has a max length of 254', 254 ) : NULL;
$content = new partial( 'form-message' );
if ( $fv->checkErrors() ) {
$content->content = $fv->displayErrors();
} else {
$this->model->uid = $this->user->id;
$this->model->code = $_POST['code'];
if ( $taxcode = $this->model->taxcodebycode ) {
$content->content = "The selected CODE is already in use by another taxcode: '$taxcode->description'";
} else {
$this->model->insert();
$content->content = $this->model->msg;
}
}
} else {
$content = new partial( 'taxcode-add-form' );
}
$this->view->title = 'Taxcode - Add';
$this->view->addView( $content );
}
come vedi prima vengono "controllati" i dati in input e poi successivamente viene fatto l'inserimento nel db e visualizzato il messaggio tramite la vista.. Qui è un po' piu complesso visto che c'è tutto un pattern che fa le operazioni..
Nessuno ti vieta di fare diversamente dipende dalle esigenze del progetto