【问题标题】:insert array to database zend 1 multiple rows将数组插入数据库zend 1多行
【发布时间】:2014-06-27 00:09:27
【问题描述】:

我想在zend 1中向数据库中插入多行,但出现错误:

致命错误:D:\HR_New\30_SourceManagement\HRSystem\library\Zend\Controller\Dispatcher\Standard.php:248Stack trace:#0 D: \HR_New\30_SourceManagement\HRSystem\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))#1 D:\HR_New\30_SourceManagement\HRSystem\library\Zend\ Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()#2 D:\HR_New\30_SourceManagement\HRSystem\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()#3 D:\ HR_New\30_SourceManagement\HRSystem\index.php(11): Zend_Application->run()#4 {main} 扔在 D:\HR_New\30_SourceManagement\HRSystem\library\Zend\Controller\Dispatcher\Standard.php 第 248 行

这是我的代码: 在 *.phtml 中,我有一个 ajax 函数:

$('#btnSubmit').bind({
        "click": function(){
            $.ajax({
                type: 'GET',
                dataType: 'JSON',
                url: 'change-authorize-data',
                data: {
                        arrCode: arrCode,
                },
                success: function(data){
                    alert("authorize success !");
                },
                error: function(xhr, ajaxOptions, thrownError){
                    alert("authorize fail!");
                    console.log("xhr.status: "+xhr.status);
                    console.log("thrownError: "+thrownError);
                }
            });
        }   
    }); 

使用 arrCode 是一个对象,其数据为:

arrCode[0][role_id]:4
arrCode[0][screen_id]:9SE902
arrCode[0][access_status]:1
arrCode[1][role_id]:5
arrCode[1][screen_id]:9SE902
arrCode[1][access_status]:1

我的控制器有一个功能:

public function changeAuthorizeDataAction(){
    $authorizeModel = new Default_Model_AuthorizeModel();
    if($this->_request->isGet()){
        $arrCode = $this->_request->getParam ( 'arrCode' );
        try {
            $authorize = $authorizeModel->addAuthorize($arrCode);
        } catch (Exception $e) {
            $authorize = $authorizeModel->updateAuthorize($arrCode);
        }

        $this->_response->setBody ( json_encode ( $authorize ) );
    }
}

这个我的模型有一个功能:

public function addAuthorize($insertArr){
     $strInsert = " insert into `tbl_authorization` (`role_id`,`screen_id`,`access_status`) values _value";
    $valueInsert = "";
    $count = count($insertArr);
    if($count > 0) {
        for($i = 0 ; $i<$count ; $i++) {
            $valueInsert .= "("
                    .$insertArr[$i]['role_id'] .",'"
                            .$insertArr[$i]['screen_id']. "',"
                                    .$insertArr[$i]['access_status'] .")";

            if($i < $count - 1) {
                $valueInsert .= ",";
            }
        }

        $newStr = str_replace("_value",$valueInsert,$strInsert);

        try {
            //die($newStr);
            $this->db->query($newStr);
        } catch (Exception $e) {

        }
    }
}

当我使用 die($newStr) 时,我得到一个 SQL。但是 $this->db->query($newStr) 没有运行。谁能帮我 ?谢谢!

【问题讨论】:

  • 在您的 $.ajax() 方法中为 url 变量指定控制器的名称以及操作。 IE。 /index/change-authorize-data

标签: php zend-framework


【解决方案1】:

我找到了原因:

$this->view->layout ()->disableLayout ();
$this->_helper->viewRenderer->setNoRender ( TRUE );

我有:

public function userauthorizingAction() {
    $userInfo = Zend_Auth::getInstance ()->getStorage ()->read ();
//add tow rows below
    $this->view->userLogin = $userInfo->employee_name;
    $this->EmpID = $userInfo->employee_id;

    $requestForm = new Default_Model_RequestformModel ();
    $this->view->role = $requestForm->getRoleUserCurent ( $this->EmpID );

    if($this->_request->isGet()){
        $sreenModel = new Default_Model_ScreenModel();
        $roleModel = new Default_Model_RoleModel();

        $this->view->screenData = $sreenModel->loadScreenList();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    相关资源
    最近更新 更多