load related parent objects from child table

Hi, I have a two tables company and category. A category may have many companies thus a one to many relation.

the company table has a child column called category.company referencing the category table by category object id.

I want to load the company object along with the category name.

Please find below screenshot of the related tables.

“Category.Company” is not a child column. It is not even a column. It is simply a “reminder” to you that the “Category” table has the “Company” column which references the “Company” table.

Based on what criteria do you want to load the company object with the category name?

Hi, Thanks for the quick response.

Ok. I want to be able to list all company details and their category name, for example i want a return something like this.

{CampanyName: “North Sea”,
Description: “”,
Email: “”,
Category: “Food”
}

Hi Ali

There are several ways to load the related data. Basically, you will need either relation auto-loading or single-step retrieval.
Please check out the documentation about Related Data retrieval

Yeah, but is the other way round. I want to load category table (parent table) from the company Table (child table). This is code am using for now, but takes a long time to load the company object, since for every loop in the company objects it will check company.object id in the category table to get the corresponding category name.

function jsonData(){
var companiesList = companiesStorage.find(dataQuery);
// this.companies = ko.mapping.fromJS(companiesList.data);
var jasonStr = JSON.stringify(companiesList.data)
var jsonObj = JSON.parse(jasonStr);

for (var i = 0, len = jsonObj.length; i < len; i++) { 

var cid = jsonObj.objectId;
var cat = getCat(cid);

var singleItem = jsonObj;
singleItem.Category = cat[0].CategoryName;

// console.log(cat[0]);

} ;

    return jsonObj;
}

function getCat(id) {
    var categoryStorage = Backendless.Persistence.of( Category);
    var dataQuery = new Backendless.DataQuery();
    dataQuery = {properties:["CategoryName"],
                 condition: "Company.objectId='" + id + "'"};
    var catlist = categoryStorage.find(dataQuery);
    
    return catlist.data;
};

Hi Ali

If you enable an auto-loading for Company’s Category column, you will be able to get your categories along with your companies in single request to the server :

var companies = companiesStorage.find(dataQuery).data;
for (var i = 0, i < companies.length - 1; i++) {
 var company = companies[і]
console.log(company.CompanyName, company.Category.CategoryName)
}

Category is not actually a column in company table, is just a reference to the category table.

Will this work. I gonna try this code now.

Tried it, but not working

Have you tried using a debugger and see where it breaks?

yeah, company.Category is undefined

Did you enable the “autoload” option for that column?

Sorry for the delay, yes I have enabled autoload for the column company under the table category.

Please let me know your app id, I’d like to check what the configuration is like.

App id : 0A085A4C-5149-4D9C-FFB8-1D596F8B5E00

App id : 0A085A4C-5149-4D9C-FFB8-1D596F8B5E00

The problem is that your Company table does not contain relation to Category table, but the Category contains Company. so to resolve your task you have to add relation Category to Company table, than you will able to load categories.

Ok. Thanks

I have three tables Company and Reviews and users.
I want to get a list of company and the total no of reviews plus the most recent review and the user who gave that review.

please see attached image

attached sample image

review.PNG

Please create a new dedicated topic for the problem.

Note that we don’t provide support in writing code, we can only help you with problems you run into while using our APIs.