Zend Framework:如何通过php数组循环在数据库中插入新数据
Zend Framework:如何通过php数组循环在数据库中插入新数据,php,database,zend-framework,loops,zend-db,Php,Database,Zend Framework,Loops,Zend Db,$this->_db->lastInsertId()为除第一个数据项之外的每个数据项返回0。 为什么?如何通过循环在数据库中插入$data数组的所有行 编辑 我的问题解决了$行=$this->fetchRow();这句话不正确。我忘了在里面加$select。用$this->fetchRow($select)替换该行解决了我的问题。我的问题解决了$行=$this->fetchRow();这句话不正确。我忘了在里面加$select。用$this->fetchRow($select)替换那一行解决了我
$this->_db->lastInsertId()代码>为除第一个数据项之外的每个数据项返回0。
为什么?如何通过循环在数据库中插入$data数组的所有行
编辑
我的问题解决了$行=$this->fetchRow();这句话不正确。我忘了在里面加$select。用$this->fetchRow($select)替换该行解决了我的问题。我的问题解决了$行=$this->fetchRow();这句话不正确。我忘了在里面加$select。用$this->fetchRow($select)替换那一行解决了我的问题。如果在if($row==false)
中放置一行,你会得到多少个echo
?@Future King如果你解决了问题-请将其作为答案发布并接受它。这将从“未回答”列表中删除该问题,您将获得额外的声誉点数
class Model_Core_NodeContents extends Zend_Db_Table_Abstract
{
protected $_name = 'node_contents';
protected $_referenceMap = array(
"Page" => array(
"columns" => array("node_id"),
"refTableClass" => "Model_Core_Node",
"refColumns" => array("id"),
"onDelete" => self::CASCADE,
"onUpdate" => self::RESTRICT
)
);
public function setContent($node_id,$key,$value){
$select = $this->select()->where("node_id = ?",$node_id)->where($this->_db->quoteInto("`key` = ?",$key));
echo $select->__toString()."<br />";
$row = $this->fetchRow();
if($row==false){
$row = $this->createRow();
$row->node_id = $node_id;
$row->key = $key;
}
$row->value = $value;
$row->save();
echo $this->_db->lastInsertId();
}
}
foreach($data as $key => $value){
$cnode = new Model_Core_NodeContents();
$cnode->setContent($id, $key, $value);
}