Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to navigate thru DataSources usind a foreign Key?

    Hi there,

    I hav two DataSources person.xml and address.xml.

    I want to make a RPC call and retrieve a person and its address (like in hibernate).
    Code:
    <Person.xml>
    ...
    <fields>
    	<field primaryKey="true" name="id" length="10" type="text"></field>
    	<field primaryKey="true" name="address" foreignKey="Address.id" length="3" type="text"></field>
    ...
    </fields>
    ...
    
    <Address.xml>
    <fields>
    	<field primaryKey="true" name="id" length="10" type="number"></field>
    	<field name="street" length="10" type="text"></field>
    	<field name="area_code" length="10" type="text"></field>
    	<field name="city" length="10" type="text"></field>
    ...
    Everything works fine but now I´m trying to get the person and its address on the same request, something like:
    Code:
    DataSource personDS = DataSource.get("PersonDS");
    Criteria personCriteria = new Criteria();
    personCriteria.addCriteria("id", 15);
    personDS.fetchData(personCriteria, new DSCallback() {
    		
    	@Override
    	public void execute(DSResponse aResponse, Object aRawData,
    			DSRequest aRequest) {
    		Record[] persons = aResponse.getData();
    		if(persons != null && persons.length == 1){
    			records[0].getAttributeAsString("name");
    
    			// I want here to get the Address DATA aswell! Is is possible?
    			records[0].getAttributeAsString("address.street");
    			records[0].getAttributeAsString("address.area_code");
    			records[0].getAttributeAsString("address.city");
    		}
    	}
    });
    Thanks in advance!

    #2
    If your persistence mechanism is SQL, use queuing (RPCManager.startQueue()) to send two fetch operations as a single HTTP request. See the Transactions folder in the EE showcase for samples.

    Comment


      #3
      Hi isomorphic,

      thanks for your reply but this is not working for me.
      I have created two requests to get the wanted data in the callBack method.
      Unfortunately, both CallBack methods are being called after my updateMyData function (Display values for decs and env are NULL).
      How can I achieve that the data will be retrieved before the update METHOD is called using RPCManager.startQueue() & Co.?

      Thanks in advance.

      Code:
      RPCManager.startQueue();
      
      DataSource clientDS = DataSource.get("DSClient");
      Criteria clientCriteria = new Criteria();
      clientCriteria.addCriteria("client", client);
      clientDS.fetchData(clientCriteria, new DSCallback() {
         		
      	@Override
          	public void execute(DSResponse aResponse, Object aRawData,
          			DSRequest aRequest) {
          		Record[] records = aResponse.getData();
          		if(records != null && records.length == 1){
          			desc = records[0].getAttributeAsString("description");
          		}
          	}
      });
      
      ...
      DataSource envDS = DataSource.get("DSEnvironment");
      Criteria envCriteria = new Criteria();
      envCriteria.addCriteria("environment", environment);
      envDS.fetchData(envCriteria, new DSCallback() {
         		
      	@Override
          	public void execute(DSResponse aResponse, Object aRawData,
          			DSRequest aRequest) {
          		Record[] records = aResponse.getData();
          		if(records != null && records.length == 1){
          			env = records[0].getAttributeAsString("environment");
          		}
          	}
      });
      RPCManager.sendQueue();
      
      updateMyData(desc, env);
      
      ...
      function void updateMyData(String description, String environment){
      ...
      }

      Comment


        #4
        fetchData() and all other network operations are asynchronous. If you don't want your method called immediately, place it in the callback.

        If that's not what you meant, please explain in more detail.

        Comment


          #5
          Yeap, I´m a dummy.
          Thanks for the idea of putting my updateMyData method inside the CallBack function! It was not the solution I was expecting but ... it works now!

          Thanks a lot :-)

          Comment

          Working...
          X