dongza5150 2013-01-17 22:20
浏览 40
已采纳

从数据库+ Zend Framework中的json加载数据

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条回答 默认 最新

  • doulin8374 2013-01-18 22:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统