logo       

Re: Best practices in a thin-controller application: msg#00145

php.zend.framework.mvc

Subject: Re: Best practices in a thin-controller application



On Wed, Jul 23, 2008 at 8:47 AM, Bryce Lohr <brycel-kr8tFwxIqV2MVkprmlnVt0B+6BGkLq7r@xxxxxxxxxxxxxxxx> 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
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise