1

我在 Zend 中遇到插入和更新查询的问题(选择没问题)。

表定义:

class Application_Model_DbTable_Kpr1Data extends Zend_Db_Table_Abstract
{
    protected $_name = 'kpr_kpr1_data';
}

这是我的数据映射器(模型)

class Application_Model_Kpr1DataMapper
{
    protected $_dbTable;

    public function setdbTable($dbTable) {
        if(is_string($dbTable)){
            $dbTable = new $dbTable();
        }
        if(!$dbTable instanceof Zend_Db_Table_Abstract ){
            throw new Exception ('Invalid table data gateway provided.');
        }
        $this->_dbTable = $dbTable;

        return $this;
    }

    public function getdbTable() {
        if (null === $this->_dbTable){
            $this->setdbTable('Application_Model_DbTable_Kpr1Data');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_Kpr1Data $kpr1data){
        $data = array('id' => (int) $kpr1data->getId(),
                'kpr1_plaza_id'   => (int) $kpr1data->getPlaza(),
                'kpr1_data' =>  new Zend_db_Expr("STR_TO_DATE('".$kpr1data->getDate()."', '%Y-%m-%d')"),
                'kpr1_money_delivered' => (float) $kpr1data->getDelivered(),
                'kpr1_money_transactions' => (float) $kpr1data->getTransactions(),
                'kpr1_created' => new Zend_Db_Expr('CURDATE()')
                );
        $id = (int) $kpr1data->getId();
        $table = $this->getdbTable();
        if (is_null($id) && $id != 0) {
            unset($data['id']);
            $table->insert($data);
        } else {
            $table->update($data, array('id => ?', $id));
        }
    }

最后一个是保存功能,应该插入和更新数据!

这个保存是从保存操作中调用的:

public function saveAction()
{
    $plazaid = (int) $this->getRequest()->getParam('plaza');
    $date = (string) $this->getRequest()->getParam('date');
    $delivered = (string) $this->getRequest()->getParam('delivered');
    $transactions = (string) $this->getRequest()->getParam('transactions');
    $kpr1data = new Application_Model_Kpr1Data();
    if ($plazaid && $date) {
        $kpr1datamapper = new Application_Model_Kpr1DataMapper();
        if($kpr1datamapper->findDatePlaza($date, $plazaid, $kpr1data)){
            $kpr1data->setDelivered($delivered)
                    ->setTransactions($transactions);
            $kpr1datamapper->save($kpr1data);
            $this->_helper->layout->disableLayout();
            $this->view->result = json_encode(array("success"=>"true"));
        } else {
            $kpr1data->setDate($date);
            $kpr1data->setDelivered($delivered);
            $kpr1data->setTransactions($transactions);
            $kpr1data->setPlaza($plazaid);
            $kpr1datamapper->save($kpr1data);
            $this->_helper->layout->disableLayout();
            $this->view->result = json_encode(array("success"=>"true"));
        }
        $this->_helper->layout->disableLayout();
        $this->view->result = json_encode(array(
                                //"success"=>"false", 
                                "errorMsg"=>"Saving error"
                              ));            
    } else {
        $this->_helper->layout->disableLayout();
        $this->view->result = json_encode(array(
                                //"success"=>"false", 
                                "errorMsg"=>"Saving error"
                              ));
    }
    return true;
}

保存动作是通过 JS 调用的,但即使通过 webbrowser 直接调用它也会失败。

行为:应用程序正在运行,当调试器运行到更新/插入行时:

    if (is_null($id) && $id != 0) {
        unset($data['id']);
        $table->insert($data);
    } else {
        $table->update($data, array('id => ?', $id));
    }

它重定向到ErrorController。

我已经检查过: 1. firePHP 没有显示这个语句 2. MySQL 数据库没有记录这个语句(我通过 general_log 功能检查过)。

我被困住了。请帮帮我。

编辑

$data=
array(6) (
  [id] => (int) 0
  [kpr1_plaza_id] => (int) 116
  [kpr1_data] => Zend_Db_Expr object {
    _expression => (string) STR_TO_DATE('2013-03-01', '%Y-%m-%d')
  }
  [kpr1_money_delivered] => (float) 120
  [kpr1_money_transactions] => (float) 122
  [kpr1_created] => Zend_Db_Expr object...

$kpr1数据=

Application_Model_Kpr1Data object {
  _plaza => (string) 116
  _date => (string) 2013-03-01
  _delivered => (string) 120.00
  _transactions => (string) 122.00
  _created => null
  _id => null
  _plazaname => null
}

这个应该做插入。下一个更新:

Application_Model_Kpr1Data object {
  _plaza => (string) 117
  _date => (string) 2013-03-01
  _delivered => (string) 120.00
  _transactions => (string) 122.00
  _created => (string) 2013-03-06 12:42:13
  _id => (string) 79
  _plazaname => (string) SPO Kraj...
4

1 回答 1

0

saveAction() $this->view->resultif/else 语句之后被覆盖,因为您的函数在(最初)设置之后不返回任何内容$this->view->result。此外设置第一个Saving error似乎是不必要的。

试试这个:

public function saveAction()
{
    $plazaid = (int) $this->getRequest()->getParam('plaza');
    $date = (string) $this->getRequest()->getParam('date');
    $delivered = (string) $this->getRequest()->getParam('delivered');
    $transactions = (string) $this->getRequest()->getParam('transactions');
    $kpr1data = new Application_Model_Kpr1Data();
    if ($plazaid && $date) {
        $kpr1datamapper = new Application_Model_Kpr1DataMapper();
        if($kpr1datamapper->findDatePlaza($date, $plazaid, $kpr1data)){
            $kpr1data->setDelivered($delivered)
                    ->setTransactions($transactions);
            $kpr1datamapper->save($kpr1data);
            $this->_helper->layout->disableLayout();
            $this->view->result = json_encode(array("success"=>"true"));
        } else {
            $kpr1data->setDate($date);
            $kpr1data->setDelivered($delivered);
            $kpr1data->setTransactions($transactions);
            $kpr1data->setPlaza($plazaid);
            $kpr1datamapper->save($kpr1data);
            $this->_helper->layout->disableLayout();
            $this->view->result = json_encode(array("success"=>"true"));
        }
    } else {
        $this->_helper->layout->disableLayout();
        $this->view->result = json_encode(array(
                                //"success"=>"false", 
                                "errorMsg"=>"Saving error"
                              ));
    }
    return true;
}

编辑:

试试这个作为你的保存操作:

public function save(Application_Model_Kpr1Data $kpr1data){
    $table = $this->getdbTable();

    if ($id == $kpr1data->getId()) {
        $data = array('id' => (int) $id,
            'kpr1_plaza_id'   => (int) $kpr1data->getPlaza(),
            'kpr1_data' =>  new Zend_Db_Expr("STR_TO_DATE('".$kpr1data->getDate()."', '%Y-%m-%d')"),
            'kpr1_money_delivered' => (float) $kpr1data->getDelivered(),
            'kpr1_money_transactions' => (float) $kpr1data->getTransactions(),
            'kpr1_created' => new Zend_Db_Expr('CURDATE()')
            );
        $table->update($data, array('id => ?', $id));
    } else {
    [...]
        $table->insert($data);
    }
}
于 2013-03-07T10:07:23.210 回答