旅行表格
$id = new Zend_Form_Element_Hidden('id');
$acc_travelplan = new Zend_Form_Element_Text('acc_travelplan');
$acc_location = new Zend_Form_Element_Text('acc_location');
$subject = new Zend_Form_Element_Text('subject');
$purpose = new Zend_Form_Element_Textarea('purpose');
$amount = new Zend_Form_Element_Text('amount');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$submit->setAttrib('class', 'btn btn-primary');
$submit->setLabel('Save As Draft');
$this->addElements(array(
$id, $acc_travelplan, $acc_location, $subject, $purpose, $amount, $submit
));
$this->setElementDecorators(array('ViewHelper'));
旅行控制器(添加行动)
$msgarray = array();
$travelmodel = new Default_Model_Travel();
$travelform = new Default_Form_Travel();
$travelform->setAttrib('action',BASE_URL.'travel/add');
/*
Create Accomodation
*/
if($this->_request->getParam('travelplan'))
{
$travelform->setDefault('acc_travelplan', $this->_request->getParam('travelplan'));
}
if($this->_request->getParam('location'))
{
$travelform->setDefault('acc_location', $this->_request->getParam('location'));
}
$this->view->form = $travelform;
if($this->getRequest()->getPost())
{
$result = $this->save($travelform);
$this->view->form = $travelform;
$this->view->msgarray = $result;
}
旅行控制器(保存操作)
public function save($travelform)
{
$result ="";
if($travelform->isValid($this->_request->getPost()))
{
$trDb = Zend_Db_Table::getDefaultAdapter();
$trDb->beginTransaction();
try
{
$travelmodel = new Default_Model_Travel();
$id = $this->_request->getParam('id');
$subject = $this->_request->getParam('subject');
$purpose = $this->_request->getParam('purpose');
$amount = $this->_request->getParam('amount');
$data = array(
'subject'=>$subject,
'purpose'=>$purpose,
'amount'=>$amount,
);
$Id = $travelmodel->SaveOrUpdateTravelData($data);
if($id)
{
$tableid = $id;
$this->_helper->getHelper("FlashMessenger")->addMessage(array("success"=>"travel updated successfully."));
}
else
{
if(!empty($this->_request->getParam('acc_travelplan')))
{
$this->saveTake($accomodationId);
}
$tableid = $Id;
$this->_helper->getHelper("FlashMessenger")->addMessage(array("success"=>"travel added successfully."));
}
$trDb->commit();
$this->_redirect('travel/index');
}
catch (Exception $e)
{
$trDb->rollBack();
}
}
else
{
$messages = $travelform->getMessages();
foreach ($messages as $key => $val)
{
foreach($val as $key2 => $val2)
{
$msgarray[$key] = $val2;
break;
}
}
return $msgarray;
}
}
public function saveTake($accomodationId)
{
$accomodationform = new Default_Form_Accomodation();
$result ="";
if($this->getRequest()->getPost())
{
try
{
$accomodationmodel = new Default_Model_Accomodation();
$id='';
$travelplan = $this->_request->getParam('acc_travelplan');
$location = $this->_request->getParam('acc_location');
$data = array(
'travelplan'=>$travelplan,
'location'=>$location,
);
$Id = $accomodationmodel->SaveOrUpdateAccomodation($data);
if($id)
{
$tableid = $id;
$this->_helper->getHelper("FlashMessenger")->addMessage(array("success"=>"Accomodation updated successfully."));
}
else
{
$tableid = $Id;
$this->_helper->getHelper("FlashMessenger")->addMessage(array("success"=>"Accomodation added successfully."));
}
}
catch (Exception $e)
{
$trDb->rollBack();
$msgarray= "Something went wrong, please try again later.";
}
}
}
保存旅行表格后,数据无法插入到住宿表中,只能插入旅行表。任何人都可以帮助我吗?
答案 0 :(得分:0)
我认为你必须把你的saveTake函数调用放在里面,如果case不在else的情况下。我的意思是在你的flash消息成功消息“旅行更新成功”之后
您可以使用$tablemodel->getAdapter()->lastInsertId();
在旅行表中插入travel_id,并将其传递给住宿表数据数组。首先在saveorupdate旅行数据之后打印$ id,如果它是插入的id,那么你不需要使用lastInsertId()只是将它传递给saveTake函数。