0

I want to load the data from a JSON file into a table. (With Zend Framework)

This is my JSON file markup:

{
    "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"
        }
}

I know how to do this with clear php: (and it works)

<?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());
            } 
        ?>

But how can I implement this in Zend Framework? What I have till now in my models map in Zend Framework:

Map "DbTable" with Districts.php in it:

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

District.php with getters and setters:

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


}

Now I have a DistrictMapper.php, but how can I implement the code to load everything from the json in my database?

What I have till now:

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

And now I need to make the save and 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)
    {

    }
1
  • 1
    So, your question is "How do I make a mapper?" Can you post what you have already tried? Commented Jan 17, 2013 at 23:22

1 Answer 1

2

From the Zend docs, Zend_DbTable::insert(), and adapted to your other posted table info:

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

Construct your array; pass it in.

Sign up to request clarification or add additional context in comments.

1 Comment

But the json I showed is just a little part of my json.... the full json can be find here: data.appsforghent.be/poi/wijken.json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.