我使用zend框架添加到db数据,如:
$data = array(
'director' =>'val' ,
'title' => 'val'
);
$this->insert($data);
我需要添加到db记录> 1.如果没有预告,它是如何做到的。
答案 0 :(得分:0)
class Application_Model_DbTable_Wti extends Zend_Db_Table_Abstract
{
protected $_name = 'wti';
public function saveWti($json_data)
{
//$db = $this->getAdapter()->query();
//$query = $this->getAdapter()->query("SELECT `date` from `{$this->_name}` order by id ASC LIMIT 1");
//$last_record = $query->fetchAll();
//$date_in_last_record = array_shift($last_record);
$query = $this->getAdapter()->query("SELECT count(*) FROM `{$this->_name}` ");
$count_records = $query->fetchAll();
$count_records = array_shift($count_records);
$count_records = $count_records['count(*)'] > 0 ? $count_records['count(*)']: 1 ;
$obj_data = json_decode($json_data);
$data_set = $obj_data->dataset;
$data = $data_set->data;
$data = array_reverse($data);
$sql = "INSERT INTO `{$this->_name}` (`date`,`open`,`high`,`low`,`settle`,`change`,`wave`,`volume`,`pdoi`,`efp`,`efs`,`block`)
VALUES";
$flag = false;
for( $start = $count_records - 1; $start < count($data); $start++ )
{
$item = $data[$start];
$date = $item[0];
$open = empty( $item[1] ) ? 0 : (float)$item[1];
$high = empty( $item[2] ) ? 0 : (float)$item[2];
$low = empty( $item[3] ) ? 0 : (float)$item[3];
$settle = empty( $item[4] ) ? 0 : (float)$item[4];
$change = empty( $item[5] ) ? 0 : (float)$item[5];
$wave = empty( $item[6] ) ? 0 : (float)$item[6];
$volume = empty( $item[7] ) ? 0 : (float)$item[7];
$pdoi = empty( $item[8] ) ? 0 : (float)$item[8];
$efp = empty( $item[9] ) ? 0 : (float)$item[9];
$efs = empty( $item[10] ) ? 0 : (float)$item[10];
$block = empty( $item[11] ) ? 0 : (float)$item[11];
$sql .= "('{$date}',{$open},{$high},{$low},{$settle},{$change},{$wave},{$volume},{$pdoi},{$efp},{$efs},{$block}),";
$flag = true;
}
if ( $flag )
{
$sql = rtrim($sql,',');
$this->getAdapter()->query($sql);
}
}
}
仅创建查询并使用$ this-&gt; getAdapter() - &gt; query($ sql);