3

My data model is described below. I would like to write a queries that would return:

  • list of reviews in a given department
  • list of specific types of training
  • list of reviews in a given college

So starting from the department, here is what I tried to do:

Query for reviews in the department:

  var department = this.store.findRecord('department',params.department_id)
  var reviews = this.store.query('review' {'faculty.department':department});

Query for specific trainings:

 var design = this.store.query('training',{'faculty.department':department, filter:{type:'ATC Design Course (DQOC)'}});

Query for the reviews of all the faculty working in a given college:

var college = this.get('college');
var cid = college.getProperties("id").id
var reviewQuery = store.query('review',{'faculty.department.college._id':college._id});
var  trainingQuery = store.query('training',{'faculty.department.college._id':college._id});

Neither of the queries give me results I am looking for. It seems they all evaluate to query that finds all results of given model type, for example query below returns all training models of all types.

 var design = this.store.query('training',{'faculty.department':department, filter:{type:'ATC Design Course (DQOC)'}});

I need a help with rewriting the queries so they will return correct results. What is the best practice in this case, and how should I implement it?

Is it possible to query model bottom app starting from reviews->faculty->department->college?

Alternatively I could go with top down approach, and query all departments, instructors and so on with some nested for loop structure but would like to go other way if possible. How would this query look like?

I will appreciate any help.

Colleges:

export default DS.Model.extend({
  name: DS.attr('string'),
  departments: DS.hasMany('department')
});

Departments

export default DS.Model.extend({
   name: DS.attr('string'),
   faculty:DS.hasMany('faculty'),
   college: DS.belongsTo('college')
});

Faculty:

export default DS.Model.extend({
  profile_image:DS.attr('string'),
  firstname: DS.attr('string'),
  lastname: DS.attr('string'),
  salutation: DS.attr('string'),
  email: DS.attr('string'),
  department: DS.belongsTo('department'),
  reviews: DS.hasMany('review'),
  training: DS.hasMany('training')
});

Reviews

export default DS.Model.extend({
  faculty: DS.belongsTo('faculty'),
  courseName:DS.attr('string'),
  internalDate:DS.attr("date"),
  externalDate:DS.attr("date"),
  funDate:DS.attr("date"),
  recertificationDate:DS.attr("date")
});

Training

export default DS.Model.extend({
  type: DS.attr('string'),
  faculty: DS.belongsTo('faculty'),
  date:DS.attr("date")
});

FIRST EDITS Question How can I get all reviews in a given college? var rquery = store.query('review', {faculty.department.college._id : college._id})

or

var rquery = store.query('review', {faculty.department.college : college})

gives me a syntax error.

SyntaxError: quality-online/components/quality-chart.js: Unexpected token (50:44)

Janusz Chudzynski
  • 2,700
  • 3
  • 33
  • 46
  • the error is because of `faculty.department.college`. You cannot pass parameters with `.` in it. if you want to send some parameter to backend you can send it as `college_id: college._id` and handle the parameter in the server side code. – Sumit Surana Nov 15 '16 at 09:50
  • Thanks for looking into it. I see. The problem is that the Review model doesn't have the college attribute. Review belongs to faculty, that belongs to department, that belongs to college. So the question is is there a way to query the parent objects this way? – Janusz Chudzynski Nov 15 '16 at 15:44
  • It could be easy to tell if you could share your model objects. In theory as there is a hierarchy here so I am guessing that review object must hold the id of its parent say instructor and instructor will hold the id of Department and the Department must hold data about College. And from what I can understand when you are going from bottom up its one to one relationship. I am assuming that your current model is review then by using `this.get('model.instructor.department.college')` should take you to the college object – Sumit Surana Nov 16 '16 at 09:19
  • You helped me to get to the answer I wanted which is a following query: store.query('training',{'faculty.department.college':college}). or store.query('review',{'faculty.department.college':college}). – Janusz Chudzynski Nov 22 '16 at 14:21
  • I will be happy to accept it if you write it as an answer – Janusz Chudzynski Nov 23 '16 at 18:53

1 Answers1

0

when using faculty.department.college._id, the query function starts looking for the definition of faculty object in your function and tries to access the value of department.college._id from it and uses the value returned by faculty.department.college._id as the parameter key. And this is the issue. As it seems the faculty object is not defined as well as this is not the right approach for passing a parameter either.

If you want to send some parameter to backend send it as 'college_id' or as mentioned in the comments above as 'faculty.department.college'. The only conditions are that it should be a string and it is handled properly at your server side code.

Sumit Surana
  • 1,554
  • 2
  • 14
  • 28
  • I just noticed that it actually doesn't work correctly. var design = this.store.query('training',{'faculty.department':department, filter:{type:'ATC Design Course (DQOC)'}}); or var improving = this.store.query('training',{'faculty.department':department, filter:{type:'Improving Your Online Course (IYOC)'}}); both are returning the same review object :( – Janusz Chudzynski Nov 30 '16 at 22:29
  • Just to make sure, did you checked or debugged if your server side code is sending different or same object, when your query is executed – Sumit Surana Dec 02 '16 at 08:56
  • How would I do it? – Janusz Chudzynski Dec 02 '16 at 17:52
  • I found a partial answer right here: http://stackoverflow.com/questions/33071340/how-to-use-emberfire-to-query-a-firebase-db-for-all-items-with-name-equalto-xyz but it doesn't explain if and how would it be possible query in a way that I want to use. – Janusz Chudzynski Dec 02 '16 at 17:56