We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42415
    • 115 Posts
    MigxLoopCollection to Json - What do I need to do to make it work?

    MODX Revolution 2.7.0-pl
    MIGX 2.12.0-pl

    Objective

    To replace (a manually updated) json file with a migxloopcollection to json output

    Why?

    I want the json to dynamically change every time the MIGXdb CMP is updated, currently I have to update the json every time an update is done to the CMP, so I'm trying to introduce a method to dynamically replace the manual update

    What I Have Done That Is Working

    I have set up a MIGXdb CMP where I can load and update data to the MySql database

    MIGXdb Schema
    <?xml version="1.0" encoding="UTF-8"?>
    <model package="towersite" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
        <object class="TowerSite" table="towersite" extends="xPDOSimpleObject">
    	  	<field key="siteid" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
    		<field key="state" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
        	<field key="township" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>        
    		<field key="address" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
    		<field key="lat" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
            <field key="lng" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>			
    		<field key="towertype" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
    		<field key="pos" dbtype="int" precision="10" phptype="integer" null="false" default="0"/>
    		<field key="createdon" dbtype="datetime" phptype="datetime" null="true"/>
        <field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
        <field key="editedon" dbtype="datetime" phptype="datetime" null="true"/>
        <field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
    		<field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
    		<field key="deletedon" dbtype="datetime" phptype="datetime" null="false" />
    		<field key="deletedby" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
        <field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />  
    		<field key="publishedon" dbtype="datetime" phptype="datetime" null="false" />
    		<field key="publishedby" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
        <aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign"/>
        <aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign"/>
        </object>
    </model>


    migxloopcollection code (this is the part I think I need help with)
    [[!migxLoopCollection?
    &packageName=`towersite`
    &classname=`TowerSite`
    &selectfields=`id,siteid,state,township,address,towertype,lat,lng`
    &toJsonPlaceholder=`markers` 
    ]]
    [[+markers]]


    json code (the original - this can work independently & it is what I am trying to replace)
    markers = [
       {
         "siteid": "BNK1001",
         "state": "Bangkok Metropolitan Area",
    	 "township": "Thonburi",
    	 "address": "21 de laetya street",
    	 "towertype": "Rooftop",
    	 "lat": 16.829394,
         "lng": 96.155004
       },
       {
         "siteid": "BNK1002",
         "state": "Bangkok Metropolitan Area",
    	 "township": "Phra Khanong",
    	 "address": "Block 5, 12 Old Khanong Road",
    	 "towertype": "Rooftop",
    	 "lat": 16.827123,
         "lng": 96.164543
       },
    ];


    I delete the 'markers json' file so I can test the migxloopcollection method, but when I load the web page it does not display the map markers and in the page source code I can see the migxloopcollection is collecting the data but the console is saying "Uncaught ReferenceError: markers is not defined". (There is a 'leaflet js' file looking for the 'markers json')

    Does anyone know what I'm doing wrong here and can advise corrective action?

    This question has been answered by jimmyjazz. See the first response.

    [ed. note: jimmyjazz last edited this post 5 years, 2 months ago.]
    • discuss.answer
      • 42415
      • 115 Posts
      Ok I got this working now by using the following code:
      [[!migxLoopCollection?
      &packageName=`towersite`
      &classname=`TowerSite`
      &selectfields=`id,siteid,state,township,address,towertype,lat,lng`
      &toJsonPlaceholder=`markers` 
      ]]
      <script>markers = [[+markers]]</script>

      I'm not sure it is the best method but it is now displaying the markers on the map.

      Next I'm going to add styling via a myTpl