2008年12月10日星期三

Re: [fw-mvc] Another Model Design Thread

-- webPragmatist <chris@webpragmatist.com> wrote
(on Tuesday, 09 December 2008, 10:58 PM -0800):
> *GULP*
>
> Hey Matthew,
> Can you just elaborate on this?
>
>
> Matthew Weier O'Phinney-3 wrote:
> >
> > Now, all that aside... you can do all of this using Zend_Db_Table as
> > your base model as well. I choose not to, but I totally understand your
> > arguments.
>
> Do you simply use the objects inside of your model?

When using Zend_Db_Table, you can define arbitrary methods, and override
existing methods. So, you might have something like the following
(untested):

class Foo extends Zend_Db_Table_Abstract
{
protected $_form;

public function setForm(Zend_Form $form)
{
$this->_form = $form;
return $this;
}

public function getForm()
{
if (null === $this->_form) {
$this->setForm(new My_Form_Foo);
}
return $this->_form;
}

public function insert(array $data)
{
$form = $this->getForm();
if (!$form->is_valid($data)) {
return false;
}

$data = $form->getValues();
return parent::insert($data);
}

public function update(array $data, $where)
{
$form = $this->getForm();
if (!$form->is_valid($data)) {
return false;
}

$data = $form->getValues();
return parent::update($data, $where);
}
}

When creating your own model class, you simply define accessors as I did
above for the form, and utilize them with your own methods.

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: