Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember self-reference and polymorphism

there is a user model which also relates to itself as contact so user has_many contacts.

Than each contact has and belongs to many "groups". Both user and contact has one address.

I read through http://lukegalea.github.io/ember_data_polymorphic_presentation/#/ couple of times, but yet still do no understand how to setup { user | contact } <-> address relationship and a contacts <-> groups association on the Ember side.

Right now I have (a simplified representation):

// Address
export default DS.Model.extend({
  city: DS.attr('string'),
  profile : DS.belongsTo('profile', { polymorphic: true, async: true })
});

// Profile
export default DS.Model.extend({
  name: DS.attr('string'),
  phone: DS.attr('string'),
  address: DS.belongsTo('address', {async: true})
});

// Contact
export default Profile.extend({
  groups: DS.hasMany('group')
});

// User
export default Profile.extend({

});

Here is the JSON

// GET /contacts
{  
  "contacts":[  
    {  
      "name":"Conrad",
      "address_id":"1",
      "id":1
    },
    {  
      "name":"Greyson",
      "address_id":"2",
      "id":2
    },
    {  
      "name":"Tommie",
      "address_id":"3",
      "id":3
    }
  ]
}

// GET /addresses
{  
  "addresses":[  
    {  
      "city":"West Lillaton",
      "profile_id":"0",
      "profile_type":"Contact",
      "id":1
    },
    {  
      "city":"Weissnatborough",
      "profile_id":"1",
      "profile_type":"Contact",
      "id":2
    },
    {  
      "city":"Predovicton",
      "profile_id":"2",
      "profile_type":"Contact",
      "id":3
    },
    {  
      "city":"VKHA",
      "profile_id":1,
      "profile_type":"User",
      "id":4
    }
  ]
}
// GET /users
{  
  "users":[  
    {  
      "name":"Emile",
      "address_id":4,
      "id":1
    }
  ]
}
like image 994
lessless Avatar asked Dec 11 '25 19:12

lessless


1 Answers

As far as I understand there is no need in polymorphic here, because you wrote: "user model which also relates to itself". You should set reflexive relation contacts for user model.

When you want to define a reflexive relation, you must either explicitly define the other side, and set the explicit inverse accordingly, and if you don't need the other side, set the inverse to null.

http://guides.emberjs.com/v1.12.0/models/defining-models/#toc_reflexive-relation

// User
export default DS.Model.extend({
  name: DS.attr('string'),
  phone: DS.attr('string'),
  address: DS.belongsTo('address', {async: true, inverse: 'user'})
  groups: DS.hasMany('group', {async: true}),
  contacts: DS.hasMany('user', { inverse: null }),
});

// Address
export default DS.Model.extend({
  city: DS.attr('string'),
  user: DS.belongsTo('user', { async: true, inverse: 'address' })
});

// Group
export default DS.Model.extend({
  name: DS.attr('string'),
  users: DS.hasMany('user', {async: true}),
});

If you'd like user and contact to be different ember models then address belongsTo polymorphic profile:

// Profile
export default DS.Model.extend({
  name: DS.attr('string'),
  phone: DS.attr('string'),
  address: DS.belongsTo('address', {async: true, inverse: 'profile'})
});

// Contact
export default Profile.extend({
  groups: DS.hasMany('group', {async: true}),
  user: DS.belongsTo('user', { async: true, inverse: 'contacts' })
});

// User
export default Profile.extend({
  contacts: DS.hasMany('contact', { inverse: 'user' }),
});

// Address
export default DS.Model.extend({
  city: DS.attr('string'),
  profile: DS.belongsTo('profile', { polymorphic: true, async: true, inverse: 'address' })
});

// Group
export default DS.Model.extend({
  name: DS.attr('string'),
  contacts: DS.hasMany('contact', {async: true}),
});

Note: proper paylod for GET addresses should be :

// GET /addresses
{"addresses":[  
  {   
    "id":1,
    "city":"West Lillaton",
    "profile": {"id:"1", "type":"Contact"},
  },{   
    "id":2,
    "city":"West Lillaton",
    "profile": {"id:"2", "type":"User"},
  }
]}
like image 144
artych Avatar answered Dec 13 '25 09:12

artych



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!