我想從JSON文件加載數據到表中。 (與Zend框架)從數據庫加載數據+ Zend Framework
這是我的JSON文件標記:
{
"wijken": {
"11": {
"coords": "3.79073170001967,51.1717753664505,0 3.79020920176376,51.1723018883706,0 3.78989543642226,51.1729670713336,0 3.78983091856725,51.1736482209016,0 3.79035112720225,51.174896701853",
"id": "kml_1",
"fid": "0",
"wijziging": "Ja",
"nieuwnr": "11",
"naam": "Noordoost",
"wijk": "Kanaaldorpen en -zone",
"wijknr": "11",
"objectid": "1",
"area": "0",
"len": "0"
}
}
我知道如何與清晰的PHP這樣做:(和它的作品)
<?php
//connection to database
$$connect = mysql_connect('localhost', 'root', 'root');
$db = mysql_select_db('testdatabase');
// ALLE VELDEN LEEGMAKEN
mysql_query("TRUNCATE TABLE wijken");
// url from json file
$url = "http://data.appsforghent.be/poi/wijken.json";
//get content from json file
$json = file_get_contents($url);
// OM ALLES VAN IN DE JSON FILE TE TONENE
//var_dump(json_decode($json));
//var_dump(json_decode($json, true));
$out = json_decode($json, true);
foreach($out["wijken"] as $wijk)
{
// ID + NAAM + WIJK + WIJKNR + COORDINATEN
$coords = addslashes($wijk[coords]);
$id = addslashes($wijk[id]);
$fid = addslashes($wijk[fid]);
$wijziging = addslashes($wijk[wijziging]);
$nieuwnr = addslashes($wijk[nieuwnr]);
$naam = addslashes($wijk[naam]);
$wijk = addslashes($wijk[wijk]);
$wijknr = addslashes($wijk[wijknr]);
$objectid = addslashes($wijk[objectid]);
$area = addslashes($wijk[area]);
$len = addslashes($wijk[len]);
mysql_query("INSERT INTO wijken (coords, id, fid, wijziging, nieuwnr, naam, wijk, wijknr, objectid, area, len)
VALUES('$coords', '$id', '$fid', '$wijziging', '$nieuwnr', '$naam', '$wijk', '$wijknr', '$objectid', '$area', '$len')") or die (mysql_error());
}
?>
但我怎麼能實現這在Zend框架? 我到現在什麼在我的模型映射在Zend框架:
地圖 「DBTABLE」 與Districts.php它:
class Backoffice_Model_DbTable_Districts extends Zend_Db_Table_Abstract
{
protected $_name = 'Wijken';
}
District.php與getter和setter方法:
class Backoffice_Model_District extends Ahs_Model_Abstract
{
protected $coords;
protected $id;
protected $fid;
protected $wijziging;
protected $nieuwnr;
protected $naam;
protected $wijk;
protected $wijknr;
protected $objectid;
protected $area;
protected $len;
public function getCoords()
{
return $this->coords;
}
public function setCoords($coords)
{
$this->coords = $coords;
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getFid()
{
return $this->fidid;
}
public function setFid($fid)
{
$this->fid = $fid;
}
public function getWijziging()
{
return $this->wijziging;
}
public function setWijziging($wijziging)
{
$this->wijziging = $wijziging;
}
public function getNieuwnr()
{
return $this->nieuwnr;
}
public function setNieuwnr($nieuwnr)
{
$this->nieuwnr = $nieuwnr;
}
public function getNaam()
{
return $this->naam;
}
public function setNaam($naam)
{
$this->naam = $naam;
}
public function getWijk()
{
return $this->wijk;
}
public function setWijk($wijk)
{
$this->wijk = $wijk;
}
public function getObjectid()
{
return $this->objectid;
}
public function setObjectid($objectid)
{
$this->objectid = $objectid;
}
public function getArea()
{
return $this->area;
}
public function setArea($area)
{
$this->area = $area;
}
public function getLen()
{
return $this->len;
}
public function setLen($len)
{
$this->len = $len;
}
}
現在我有了一個DistrictMapper.php,但是我怎樣才能實現代碼來從我的數據庫中的json加載所有東西?
我有什麼至今:
protected $_dbTable;
public function __construct()
{
$this->_dbTable = new Backoffice_Model_DbTable_Districts();
}
public function fetchAll()
{
$rowset = $this->_dbTable->fetchAll();
$districts = $this->_toObjects($rowset);
return $districts;
}
現在我需要做的保存和toobject。
public function save(Backoffice_Model_Admin $admin)
{
$data = array('coords' => $district->getCoords(),
'id' => $district->getId(),
'fid' => $district->getFid(),
'wijziging' => $district->getWijziging(),
'nieuwnr' => $district->getNieuwnr(),
'naam' => $district->getNaam(),
'wijk' => $district->getWijk(),
'wijknr' => $district->getWijknr(),
'objectid' => $district->getObjectid(),
'area' => $district->getArea(),
'len' => $district->getLen(),
);
}
protected function _toObject(Zend_Db_Table_Row_Abstract $row = null)
{
}
所以,你的問題是「我如何製作一個映射器?」你能發佈你已經嘗試過的嗎? –
添加它做我的文章 – nielsv