2013-01-22 22 views
0

我想從我的數據庫中的2個json文件加載數據。 JSON-文件1所述的標記:從數據庫中加載json數據+調用未定義的方法錯誤+ Zend Framework

{ 
    "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,0 3.79089521566258,51.1755715564749", 
      "id": "kml_1", 
      "fid": "0", 
      "wijziging": "Ja", 
      "nieuwnr": "11", 
      "naam": "Noordoost", 
      "wijk": "Kanaaldorpen en -zone", 
      "wijknr": "11", 
      "objectid": "1", 
      "area": "0", 
      "len": "0" 
     }, 
     "12": { 
      "coords": "3.78365655007178,51.0788871010895,0 3.78236442099369,51.078059164252,0 3.78145483809257,51.0772221613719,0 3.77888089082956,51.0750304363252,0 3.77670256394985,51.0734391195385,0 3.77279418332369,51.0714074143415", 
      "id": "kml_2", 
      "fid": "1", 
      "wijziging": "Ja", 
      "nieuwnr": "12", 
      "naam": "Noordoost", 
      "wijk": "Oostakker", 
      "wijknr": "12", 
      "objectid": "2", 
      "area": "0", 
      "len": "0" 
     }, 
     "14": { 
      "coords": "3.75380711028508,51.0773171406482,0 3.75436966210847,51.0771805986634,0 3.75572764083826,51.077128950619,0 3.75568978579317,51.0768781807292,0 3.75764106686573,51.0767833829573,0 3.75931252603489,51.0764226285855", 
      "id": "kml_3", 
      "fid": "2", 
      "wijziging": "Ja", 
      "nieuwnr": "14", 
      "naam": "Noordoost", 
      "wijk": "Sint Amandsberg", 
      "wijknr": "14", 
      "objectid": "3", 
      "area": "0", 
      "len": "0" 
     }, 
     ...... 
} 

JSON文件2所述的標記:

{ 
    "geboortes": [ 
     { 
      "wijk": "Binnenstad", 
      "year_1999": 167, 
      "year_2000": 156, 
      "year_2001": 167, 
      "year_2002": 179, 
      "year_2003": 150, 
      "year_2004": 196, 
      "year_2005": 187, 
      "year_2006": 204, 
      "year_2007": 189, 
      "year_2008": 206, 
      "year_2009": 218 
     }, 
     { 
      "wijk": "Bloemekenswijk", 
      "year_1999": 106, 
      "year_2000": 105, 
      "year_2001": 106, 
      "year_2002": 101, 
      "year_2003": 125, 
      "year_2004": 133, 
      "year_2005": 138, 
      "year_2006": 138, 
      "year_2007": 120, 
      "year_2008": 177, 
      "year_2009": 143 
     }, 
     { 
      "wijk": "Brugse Poort - Rooigem", 
      "year_1999": 231, 
      "year_2000": 219, 
      "year_2001": 198, 
      "year_2002": 205, 
      "year_2003": 207, 
      "year_2004": 206, 
      "year_2005": 256, 
      "year_2006": 277, 
      "year_2007": 258, 
      "year_2008": 298, 
      "year_2009": 295 
     }, 
     ..... 
} 

我的兩個表:

  • "Districts" with fields (= fields of the json): coords, id, fid, wijziging, nieuwnr, naam, wijk, wijknr, objectid, area, len

  • "Births" with fields (= fields of the json): wijk, year_1999, year_2000, year_2001, year_2002, year_2003, year_2004, year_2005, year_2006, year_2007, year_2008, year_2009

我其中我加載一個updateAction來自2個表中的兩個jsons的數據:

public function updateAction() 
    { 

     $view = $this->view; 
     $view->title = $this->_translate->_('Edit') . ' - ' . $this->_translate->_('Back office'); 


     $url = "http://data.appsforghent.be/poi/wijken.json"; 
     $url2 = "http://data.appsforghent.be/demografie/geboortes.json"; 
     $url3 = "http://data.appsforghent.be/demografie/overlijdens.json"; 

     $json = file_get_contents($url); 
     $json2 = file_get_contents($url2); 

     $out = Zend_Json::decode($json); 
     $out2 = Zend_Json::decode($json2); 

     // WIJKEN JSON 
     foreach($out["wijken"] as $w) 
    {   
      $coords = addslashes($w['coords']); 
      $id = addslashes($w['id']); 
      $fid = addslashes($w['fid']); 
      $wijziging = addslashes($w['wijziging']); 
      $nieuwnr = addslashes($w['nieuwnr']); 
      $naam = addslashes($w['naam']); 
      $wijk = addslashes($w['wijk']); 
      $wijknr = addslashes($w['wijknr']); 
      $objectid = addslashes($w['objectid']); 
      $area = addslashes($w['area']); 
      $len = addslashes($w['len']); 

     $data = array('coords' => $coords, 
         'id' => $id, 
         'fid'  => $fid, 
         'wijziging' => $wijziging, 
         'nieuwnr' => $nieuwnr, 
         'naam' => $naam, 
         'wijk' => $wijk, 
         'wijknr' => $wijknr, 
         'objectid' => $objectid, 
         'area' => $area, 
         'len' => $len,  
     ); 

     $district = new Backoffice_Model_District($data); 

     $districtMapper = new Backoffice_Model_DistrictMapper(); 
     $districtMapper->save($district); 

     } 

     // GEBOORTES JSON 
     foreach($out2["geboortes"] as $g) 
    {   
      $wijk = addslashes($g['wijk']); 
      $year_1999 = addslashes($g['year_1999']); 
      $year_2000 = addslashes($g['year_2000']); 
      $year_2001 = addslashes($g['year_2001']); 
      $year_2002 = addslashes($g['year_2002']); 
      $year_2003 = addslashes($g['year_2003']); 
      $year_2004 = addslashes($g['year_2004']); 
      $year_2005 = addslashes($g['year_2005']); 
      $year_2006 = addslashes($g['year_2006']); 
      $year_2007 = addslashes($g['year_2007']); 
      $year_2008 = addslashes($g['year_2008']); 
      $year_2009 = addslashes($g['year_2009']); 

     $data2 = array('wijk'  => $wijk, 
         'year_1999' => $year_1999, 
         'year_2000' => $year_2000, 
         'year_2001' => $year_2001, 
         'year_2002' => $year_2002, 
         'year_2003' => $year_2003, 
         'year_2004' => $year_2004, 
         'year_2005' => $year_2005, 
         'year_2006' => $year_2006, 
         'year_2007' => $year_2007, 
         'year_2008' => $year_2008, 
         'year_2009' => $year_2009, 

     ); 

     $birth = new Backoffice_Model_Birth($data2); 

     $birthMapper = new Backoffice_Model_BirthMapper(); 
     $birthMapper->save($birth); 

     } 

     echo "Worked!"; 

    } 

保存在我的DistrictMapper功能:(同爲BirthMapper但不同的值)

public function save(Backoffice_Model_District $district) 
    { 


     $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(), 
     ); 

他從JSON加載數據到表1,但是當他想json2裝入到表2我得到這個錯誤:

Fatal error: Call to undefined method Backoffice_Model_Birth::setYear_1999() in /Applications/MAMP/htdocs/StaGent/library/Ahs/Model/Abstract.php on line 40

我Abstract.php

abstract class Ahs_Model_Abstract 
{ 
    /** 
    * @param array $values 
    */ 
    public function __construct(array $values) { 
     foreach($values as $key => $value) { 
      $setter = 'set' . ucfirst($key); 
      $this->{$setter}($value); 
     } 
    } 
} 
+1

發佈Applications/MAMP/htdocs/StaGent/library/Ahs/Model/Abstract.php的內容 –

回答

0

我離開了下劃線了我的變量,現在它的作品!

相關問題