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

时间:2016-10-25 02:52:12

标签: php mysql zend-framework

对所有人来说,美好的一天。我在数据库中有2个表,第一个表是Travel表包含id,主题,目的,金额。第二个表是住宿包含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'));

旅行控制器(添加行动)

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

保存旅行表格后,数据无法插入到住宿表中,只能插入旅行表。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我认为你必须把你的saveTake函数调用放在里面,如果case不在else的情况下。我的意思是在你的flash消息成功消息“旅行更新成功”之后

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