我在Zend中插入和更新查询时遇到问题(Select ok)。表Zend更新/插入(Zend_Db_Table_Abstract)不起作用
定义:
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;
}
Save操作是通过JS调用,但即使是直接通过网页浏览器失败调用。
行为:应用程序正在运行,并在调试器运行到更新/插入行:
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...
能否请您提供的'$ data'转储内'保存()' – simplyray 2013-03-07 09:46:23
附在主邮件 – mgrom 2013-03-07 10:27:35