Zend update / insert(Zend_Db_Table_Abstract)不起作用

时间:2013-03-07 08:03:19

标签: zend-framework sql-update zend-db-table sql-insert

我在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...

$ kpr1data =

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...

1 个答案:

答案 0 :(得分:0)

由于你的函数在(最初)设置saveAction()后没有返回任何内容,因此$this->view->result $this->view->result中的

会被if / else语句覆盖。 此外,设置第一个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);
    }
}