2008年7月22日星期二

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



On Tue, Jul 22, 2008 at 11:34 AM, Adam Jensen <jazzslider@gmail.com> wrote:
And you're absolutely right about the varying subclasses of exceptions
for varying failure conditions...in my controller, it allows me to do
something like this:

[code]
public function viewAction()
{
   try {
       $this->view->trip = new Travel_Trip($this->_getParam('id', null));
   } catch (Travel_Trip_Exception_MissingIdentifier $e) {
       $this->_setParam('message', 'You must provide a trip ID.');
       $this->_forward('bad-request', 'error', 'default');
       return;
   } catch (Travel_Trip_Exception_InvalidIdentifier $e) {
       $this->_setParam('message', 'Invalid trip ID!');
       $this->_forward('bad-request', 'error', 'default');
       return;
   } catch (Travel_Trip_Exception_NotFound $e) {
       $this->_forward('not-found', 'error', 'default');
       return;
   }
}
[/code]
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.

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

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

没有评论: