Question

I've got a problem with insert and update queries in Zend (Select is ok).

Definition of table:

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

Here is my data mapper (model)

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

The last one is save function that should insert and update the data!

And this save is called from save action:

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

Save action is called via JS, but even called directly through webbrowser it fails.

Behaviour: Application is running, and when debugger runs into update/insert line:

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

it's redirecting to ErrorController.

I've check that: 1. firePHP is not showing this statements 2. MySQL database doesn't log this statement (I've checked via general_log feature).

I'm stucked. Help me please.

edit

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

This one should do insert. And next one update:

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...
Was it helpful?

Solution

in your saveAction() $this->view->result gets overwritten after if/else statement since your function does not return anything after (initially) setting $this->view->result. Furthermore setting the first Saving error seems to be needless.

Try this:

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

EDIT:

Try this as your save action:

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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top