I have several models that I am working with:
invoice-group.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
primary: DS.attr('number'),
//relationships
invoiceGroupMembers: DS.hasMany('invoiceGroupMember'),
billables: DS.hasMany('billable'),
matter: DS.belongsTo('matter')
});
invoice-group-member.js
import DS from 'ember-data';
export default DS.Model.extend({
billSplit: DS.attr('number'),
// relationships
invoiceGroup: DS.belongsTo('invoiceGroup'),
firm: DS.belongsTo('firm')
});
firm.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
tmNum: DS.attr('number'),
phone: DS.attr('string'),
address: DS.attr('string'),
suite: DS.attr('string'),
city: DS.attr('string'),
state: DS.attr('string'),
zip: DS.attr('string'),
region: DS.attr('string'),
industry: DS.attr('string'),
rating: DS.attr('string'),
scBusiness: DS.attr('string'),
// relationships
invoiceGroupMembers: DS.hasMany('invoiceGroupMembers')
});
firm-has-matter.js
import DS from 'ember-data';
export default DS.Model.extend({
//define me so they are available in a model:instance
billSplit: DS.attr('number'),
primary: DS.attr('string'),
firmMatterNumber: DS.attr('string'),
status: DS.attr('string'),
//relationships
firm: DS.belongsTo('firm'),
matter: DS.belongsTo('matter')
});
So in the template, I need to loop through the invoice-groups, and inside of that loop i need to get all of the loop through all of the firms that are assigned to the particular matter. The problems is that invoice-groups model has a matter attribute, and firm-has-matter has a matter attribute, but i don't know how to relate the two together so that i can loop through the firms that are related with the matter that is set in the invoice group record.
this is pseudocode, but I would like to be able to do something like:
{{#each ig in invoice_groups}}
{{input style="float:left;" value=ig.name type="text" name="name"}}
{{#each firm in ig.firms}}
{{/each}}
{{/each}}
Obviously, no such relationship exists, but I was just wondering how you would go about facilitating such a relationship while still using Ember data, or is it something where I am going to have to break with Ember data and build and object in the controller that I pass into the template.
I am converting an existing app from codeigniter, to restapi with an ember client side. In the existing app, we are just querying everything we need and manipulating the data in the model to pass a custom array into the view.
public function getMatterGroups($matters_id)
{
$invoice_groups = $this->db->select("i.*")
->from('invoice_groups as i')
->where('matters_id', $matters_id)
->get()
->result_array();
//format for easy access and prep firms
$groups = array();
foreach ($invoice_groups as $group) {
$group['firms'] = array();
$groups[$group['id']] = $group;
}
$members = $this->db->select("m.*, f.name as firm_name")
->from('invoice_group_members as m')
->join('invoice_groups as i', 'i.id = m.invoice_groups_id')
->join('firms as f', 'f.id = m.firms_id')
->where('matters_id', $matters_id)
->get()
->result_array();
// now load all related firms into that invoice group
foreach ($members as $member) {
// register the firm with the invoice group
$groups[$member['invoice_groups_id']]['firms'][] = $member;
}
// count all invoices for each group
foreach ($groups as $key => $group) {
$group['invoice_count'] = $this->db->select("id")
->from('invoices')
->where('invoice_groups_id', $key)
->where('matters_id', $matters_id)
->get()
->num_rows();
// update group
$groups[$key] = $group;
}
return $groups;
}
So ultimately we end up with an array like so:
$invoice_groups = array(
2 => array(
'id' => '2',
'matters_id' => '378',
'name' => 'All',
'primary' => 0,
'firms' => array(
'0' => array(
'id' => '3',
'invoice_groups_id' => '2',
'firms_id' => '34',
'bill_split' => '16.0000',
'firm_name' => 'FIRMS NAME'
),
'1' => array(
etc.
),
)
),
);
So how can you achieve this using Ember-data, and if that is not possible, how would you accomplish this in your controller or route?
Aucun commentaire:
Enregistrer un commentaire