Naw I have a newAction() which just delegates to editAction()
ZF renders the view for the action that was dispatched so I do some overrides to the form in the view scripts before it renders, which takes care of buttons saying "Create" vs "save".
I meant in my case the editAction would be 100s of lines if not broken down into template methods, when I have it broken into an "API" it is only 40 lines, it is much easier for me to override 1 off template methods and have 1 place to maintain the complexity. it is a design principle called inversion of control. Yes indirection is 1 of the costs to abstraction, and no there is no right or wrong method to doing your project.
Here is an example where I needed to change the "guts" of the editAction(). Here is without a template method:
Code: Select all
public function editAction()
{
if( $this->_getParam( 'employer' ) )
{
$invoice = new Invoice();
$invoice->setEmployer( $this->getMapper( 'Employer' )->find( $this->_getParam( 'employer' ) ) );
}
else
{
$invoice = $this->findModel();
}
$this->setBreadcrumbOptions( 'admin-invoice-edit', array(
'label' => $invoice->getTitle(),
'params' => array(
'id' => $invoice->getId()
)
));
/***/
$form = new Invoice_Form( array(
'invoice' => $invoice
));
$form->populateFrom( $invoice );
if( $this->getRequest()->isPost() )
{
if( $form->isValid( $this->getRequest()->getPost() ) )
{
$form->populateTo( $invoice );
$invoice->setCreated( new Zend_Date );
$this->getMapper()->save( $invoice );
$this->flashMessenger->addMessage( 'Invoice Saved' );
return $this->_redirect( $this->url( array(
'module' => 'Admin',
'controller' => 'Invoice',
'action' => 'index'
), 'default', true ) );
}
}
/****/
$this->view->id = $invoice->getId();
$this->view->invoice = $invoice;
$this->view->employer = $invoice->getEmployer();
$form->addElement( 'text', 'employer', array(
'value' => $invoice->getEmployer(),
'disabled' => 'disabled',
'label' => 'Employer',
'order' => 1
));
$this->view->form = $form;
}
The part between the comments is what I needed to change. ( as well as the $this->view part and adding an element to the form ). This was written before my refactorings. Now I will go back and eliminate most of that code, the only code that really needs to be overriden:
in the TM for form creation:
$invoice->setCreated( new Zend_Date );
Code: Select all
$form->addElement( 'text', 'employer', array(
'value' => $invoice->getEmployer(),
'disabled' => 'disabled',
'label' => 'Employer',
'order' => 1
));
TM for setting up view
Code: Select all
$this->view->id = $invoice->getId();
$this->view->invoice = $invoice;
$this->view->employer = $invoice->getEmployer();
TM for findOrCreateModel
Code: Select all
# if( $this->_getParam( 'employer' ) )
# {
# $invoice = new Invoice();
# $invoice->setEmployer( $this->getMapper( 'Employer' )->find( $this->_getParam( 'employer' ) ) );
# }
# else
# {
# $invoice = $this->findModel();
# }
and lastly implement doSave
Code: Select all
$invoice->setCreated( new Zend_Date );
Having the code all inline can also have consequences of making it harder to read thru IMO, as you can see I am able to override the spots I need to
The downside to having the huge method is my success message, redirect, etc.. are all not encapsulated. To repeat this scheme w/o breaking into separate methods thru 30 controllers would have meant major problems for my project
I recommend reading
http://en.wikipedia.org/wiki/Hollywood_Principle
and
http://www.matthewtmead.com/blog/?p=8