2008年7月23日星期三

Re: [fw-mvc] Best practices in a thin-controller application



On Wed, Jul 23, 2008 at 8:47 AM, Bryce Lohr <brycel@patientadvocate.org> wrote:
HI David,


David Mintz wrote:
Which brings me to another question, maybe sort of OT. If your model's constructor requires an id parameter, how do you instantiate a "blank" object to be populated with data provided by the user and then -- excuse me for thinking database -- inserted into a table? I am thinking of RDMSes that have auto-incrementing.You find out the id after the insert.


One possibility would be to use a Special Case [1] object to encapsulate the "blank object" concept. The special case should be interchangeable with the the "real" object.

[1] http://martinfowler.com/eaaCatalog/specialCase.html


OK, what about

class MyModel {

  protected $table;
  protected $row;

  function __construct ($id = null) {

       $this->table = new My_Subclass_Of_Zend_Db_Table_Abstract();
       if ($id) { // pretend you validated, for brevity's sake
          $this->row = $this->table->find($id)->current();
          if (!$this->row) throw new Exception("d'oh!"); // or throw your subclass
      } else {
          $this->row = $this->table->createRow();
      }
}


--
David Mintz
http://davidmintz.org/

The subtle source is clear and bright
The tributary streams flow through the darkness

没有评论: