gpt4 book ai didi

php - 在zend中使用单一表单插入两个表的数据

转载 作者:行者123 更新时间:2023-11-29 20:09:23 26 4
gpt4 key购买 nike

祝大家有美好的一天。我在数据库中有 2 个表,第一个表是 Travel 表,包含 id、主题、目的、金额。第二个表是Accomodation,包含id、travelplan、location、travel_id。到目前为止,我已尝试在“旅行”表单中插入“旅行和住宿”表的数据,但始终失败。

旅行表格

$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'));

旅行 Controller (添加操作)

$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;
}

旅行 Controller (保存操作)

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.";
}
}
}

保存旅行表单后,数据无法插入住宿表,只能插入旅行表。有人可以帮助我吗?

最佳答案

我认为你必须将 saveTake 函数调用放在 if case 内而不是 else case 内。我的意思是在您的 Flash Messager 成功消息“旅行更新成功”之后

您可以使用$tablemodel->getAdapter()->lastInsertId();在旅行表中获取插入的travel_id并将其传递给Accomodation表数据数组。在 saveorupdate 旅行数据后首先打印 $id 如果它是插入的 id 那么您不需要使用 lastInsertId() 只需将其传递给 saveTake 函数即可。

关于php - 在zend中使用单一表单插入两个表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40230610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com