0

JSONファイルからテーブルにデータをロードしたいと思います。(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 Frameworkに実装できますか?Zend Frameworkのモデルマップにこれまでにあるもの:

Districts.phpを含む「DbTable」をマップします。

    class Backoffice_Model_DbTable_Districts extends Zend_Db_Table_Abstract
{
    protected $_name = 'Wijken';
}

ゲッターとセッターを使用したDistrict.php:

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;
}

そして今、私はsaveと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)
    {

    }
4

1 に答える 1

2

Zendドキュメントから、Zend_DbTable :: insert()、および他の投稿されたテーブル情報に適合:

public function save(Backoffice_Model_Admin $admin){ 
  $data = array(
    "fid": "0",
    "wijziging": "Ja",
    "nieuwnr": "11",
    // other data from $admin...
  );
  $table = new Backoffice_Model_DbTable_Districts();
  $table->insert($data);
}

配列を作成します。それを渡します。

于 2013-01-18T22:05:57.807 に答える