I’m quite new to Zend & JSON, however I have the need to learn.
What I want to achieve is: having a Dojo filteringselect (with
autocomplete) control which is linked to zipcodes from a database
(and which keeps track of the ID, so I can store that ID as a FK in
another table (later on). The structure is MVC. I do get results
from the database, however I can’t seem to make it shine. Nothing
shows up in the filteringselect control. So basicly the structure
field of my database needs to get into the filteringsselect control
and keeping track of that id, because I need it later on as a FK in
another table. Please help me out!
表:
<?php
class Application_Model_Place extends FS_Model_Popo {
protected $_fields = array(
'id' => NULL,
'zip' => NULL,
'name' => NULL,
'up' => NULL,
'structure' => NULL);
protected $_primaryKey = array('id');
}
形:
$place = new Zend_Dojo_Form_Element_FilteringSelect('Place');
$place->setLabel('Place')
->setAttrib('title', 'Add a place.')
->setAutoComplete(true)
->setStoreId('placeStore')
->setStoreType('dojox.data.QueryReadStore')
->setStoreParams(array('url' => '/graph/place/autocomplete'))
->setAttrib("searchAttr", "structure")
->setRequired(true);
コントローラ:
class Graph_PlaceController extends Zend_Controller_Action {
public function autocompleteAction() {
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$structuur = $this->_getParam("structure", "");
$results = FS_Model_Factory::getInstance()->place->autocomplete(array('structure like ?'=> "%".$structure."%"));
$enc_res = array();
foreach ($results as $value) {
array_push($enc_res,$this->_helper->convert->toArray($value));
}
$this->_helper->json($enc_res);
$data = new Zend_Dojo_Data('id', $enc_res);
$this->_helper->autoCompleteDojo($data);
}
}
json($ enc_res)の例は次のようになります。
{"id":"235","zip":"3130","name":"Betekom","up":"BETEKOM","structure":"3130 Betekom"}, {"id":"268","zip":"3211","name":"Binkom","up":"BINKOM","structure":"3211 Binkom"},{"id":"377","zip":"3840","name":"Broekom","up":"BROEKOM","structure":"3840 Broekom"},{"id":"393","zip":"1081","name":"Brussel (Koekelberg)","up":"BRUSSEL (KOEKELBERG)","structure":"1081 BRUSSEL (KOEKELBERG)"},{"id":"421","zip":"1081","name":"Bruxelles (Koekelberg)","up":"BRUXELLES (KOEKELBERG)","structure":"1081 BRUXELLES (KOEKELBERG)"},{"id":"668","zip":"3670","name":"Ellikom","up":"ELLIKOM","structure":"3670 Ellikom"},{"id":"1236","zip":"3840","name":"Jesseren (Kolmont)","up":"JESSEREN (KOLMONT)","structure":"3840 Jesseren (Kolmont)"},{"id":"1275","zip":"3370","name":"Kerkom","up":"KERKOM","structure":"3370 Kerkom"}
私はいくつかの選択肢があると思います:
-
Either you have control on the structure of the json you produce
on your controller, and therefore you should generate the format
expected by dojox.store.QueryReadStore (which is by default the
same as dojo.data.ItemFileReadStore). See http://dojotoolkit.org/reference-guide/dojo/data/ItemFileReadStore.html#general-structure -
Or you create a custom store that understands the structure of
your json response. See http://dojotoolkit.org/reference-guide/dojox/data/QueryReadStore.html#query-translation -
Or you are using dojo >1.6, and you can use
dojo.store.JsonRest with its companion dojo.data.ObjectStore as
described here
オプション1は明らかに最も簡単です…