Results 1 to 2 of 2

Thread: Access model Hasmany relation in grid column

  1. #1
    Sencha User
    Join Date
    Apr 2015
    Posts
    1

    Default Access model Hasmany relation in grid column

    I have the following two tables:

    Table company with this columns:

    id
    name
    cif
    location


    Table worker with this columns:
    id
    idCompany
    name
    code

    The relationship is that one company has many workers
    In extjs 4.2 I have this two models:
    mdlCompany
    Code:
    Ext.define('App.model.mdlCompany', { 
    
        extend: 'Ext.data.Model',
        fields: [
            {name: 'id',            type: 'integer'},
            {name: 'name',          type: 'string'},
            {name: 'cif',           type: 'string'},
            {name: 'location',     type: 'string'}
        ]
    });
    mdlWorker

    Code:
    Ext.define('App.model.mdlWorker', {
        extend: 'Ext.data.Model',
        fields: [
            {name: 'id',            type: 'integer'},
            {name: 'name',          type: 'string'},
            {name: 'code',          type: 'string'}
        ]
    });
    How can I set the relations beetween the models? Im trying this line set up in mdlCompany:
    Code:
    hasMany: {model: 'App.model.mdlWorker', foreignKey: 'idCompany'}
    And in mdlWorker:
    Code:
    {name: 'idCompany',     type: 'int'}
    Is this the correct way?

    The return of the php that gets the data is:

    Code:
    0:{
        id: 1
        name: "John",
        code: "J001",
        company:{
           id:1
           name: "Company 1",
           location: "Some place"}
    }
    1:{
        id: 2
        name: "Paul",
        code: "P001",
        company:{
           id:2
           name: "Company 2",
           location: "Some other place"}
    }
    ...
    I have this grid:
    Code:
    Ext.apply(this, {     store: 'somestore with mdlWorker assigned',
        stripeRows: true,
        loadMask: true,
        columns: [
            { text: 'Id',           dataIndex: 'id',            flex: 1 },
            { text: 'Name',         dataIndex: 'name',          flex: 1 },
            { text: 'Code',         dataIndex: 'code',          flex: 1 },
            { text: 'Company',      dataIndex: 'company.name',  flex: 1 }
        ],
    });
    But the column Company is empty when I render the grid
    and load its store. Do I need some more configuration? Or How can I
    access the company.name property of mdlWorker?

  2. #2
    Sencha User
    Join Date
    Sep 2018
    Posts
    1

    Default

    I hope someone could help you, I had several problems triying to load nested data with relational models.
    Last edited by esmomo; 20 Dec 2018 at 1:04 AM. Reason: wrong

Similar Threads

  1. Replies: 3
    Last Post: 15 Jan 2014, 2:52 AM
  2. hasmany relation two models one store trouble bubble
    By dishwashwebdesign in forum Sencha Touch 2.x: Q&A
    Replies: 3
    Last Post: 17 Jul 2013, 2:22 PM
  3. Store Data with hasMany relation
    By jason rogers in forum Sencha Touch 2.x: Q&A
    Replies: 2
    Last Post: 11 Apr 2012, 1:11 PM
  4. Hasmany relation with a single element
    By Mohammad Aruri in forum Ext: Q&A
    Replies: 0
    Last Post: 28 Sep 2011, 10:32 PM
  5. Replies: 0
    Last Post: 17 Aug 2011, 7:19 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •