1. #1
    Sencha User
    Join Date
    Dec 2011
    Posts
    222
    Vote Rating
    9
    dedoz will become famous soon enough

      0  

    Default Working with associated models #3 - hasOne conceptual (docs) problems

    Working with associated models #3 - hasOne conceptual (docs) problems


    This applies to docs for ExtJs 4.1 beta 1, beta 2 and beta 3.
    (as always excuse my bad english)

    Im terribly wrong or “Has One” relationship is terrible bad defined from the docs to the code?

    There were longs posts in the premium forums asking for a has one association type, so Extjs give them this association but it seems like was a copy / paste of docs and code from “belongs to” and they put a “has one” association, “work done, next ?”
    First check the documentation for “has one” association (docs for 4.1 beta 1, 2 and 3)
    Wrong thing #1
    “Has one” docs, at the beginning says “The owner model is expected to have a foreign key which references the primary key of the associated model.” Seems to be a copy/paste from “belongs To” docs, anyway this could be valid , but it’s not, “has one” is similar (in some way) to “belongsTo” but differs in “where the foreign key is”.
    The type of associations tells where the foreign is placed.
    • Model A belongs to Model B places the foreign key in Model A. This is a relation “one to one” from A to B (from B to A is another history).
    • Model A has Many Model B places the foreign key in model B, also you expect a group of models B associated to Model A. This a relation “one to many” from A to B.
    • Model A has One model B places the foreign key in Model B, also you expect at max only one model B associated to Model A.
    This way “has one” and “belongs to” are very similar, they both represent a one to one relation but the diference is where the foreign key is placed.
    • Model A “belongsTo” Model B translates to A has a relation one to one with B , and B primary key travels to A, A has a field B_id.
    • Model A “HasOne” Model B translates to A has a relation one to one with B , and A primary key travels to B, B has a field A_id.
    So “hasOne” docs says (note that owner model is A and associated model is
    The owner model is expected to have a foreign key which references the primary key of the associated model.” (seems like this was copied from belongs to docs)
    Should say “The associated model is expected to have a foreign key which references the primary key of the owner model

    its important to have the correct concept of “has one” because theres more problems in the docs and worst theres problems in the source code that ill post later.

    So anyone else agrees that “has one” docs are wrong or im wrong D:

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,004
    Vote Rating
    847
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    If you feel you have found a bug then you can post in the bugs forum.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Jun 2011
    Posts
    36
    Vote Rating
    0
    newuser7000 is on a distinguished road

      0  

    Default Another example similar to test

    Another example similar to test


    Hi there, I have an working example using JSON. With XML, with nested data, ExtJS 4.1 has serious problem to face with associations: HasMany, HasOne.
    ------------------------------
    Cheers, W.

    Code:
    {
        "id": 1,
        "nome": "Loiane",
        "sobrenome": "Groner",
        "telefones": [
            {
                "id": 12,
                "ddd": "11",
                "numero": "1234-5678"
            },
            {
                "id": 13,
                "ddd": "19",
                "numero": "9876-1234"
            }
        ],
        "endereco": {
            "id": 14,
            "logradouro": "rua ficticia",
            "numero": "100",
            "complemento": "apt 10"
        }
    }

    Code:
        Ext.define('Pessoa',{
            extend: 'Ext.data.Model',
    
    
            fields: [
                {name: 'id', type: 'int'},
                {name: 'nome', type: 'string'},
                {name: 'sobrenome', type: 'string'}
            ],
    
    
            proxy: {
                type: 'rest',
                url: 'dados',
                format: 'json'
            },
    
    
            hasMany: {model: 'Telefone', foreignKey: 'pessoaId'},
    
    
            hasOne: {model: 'Endereco', foreignKey: 'pessoaId'}
        });
    
    
        Ext.define('Telefone',{
            extend: 'Ext.data.Model',
    
    
            fields: [
                {name: 'id', type: 'int'},
                {name: 'ddd', type: 'string'},
                {name: 'numero', type: 'string'},
                {name: 'pessoaId', type: 'int'}
            ],
    
    
            belongsTo: {model: 'Pessoa', foreignKey: 'pessoaId'}
        });
    
    
        Ext.define('Endereco',{
            extend: 'Ext.data.Model',
    
    
            fields: [
                {name: 'id', type: 'int'},
                {name: 'logradouro', type: 'string'},
                {name: 'numero', type: 'string'},
                {name: 'complemento', type: 'string'},
                {name: 'pessoaId', type: 'int'}
            ]
        });
        
        Ext.onReady(function(){
    
    
            Pessoa.load(1,{
    
    
                success: function (pessoa){
    
    
                    console.log('Nome da pessoa é ' + pessoa.get('nome'));
    
    
                    var telefones = pessoa.telefones();
    
    
                    telefones.each(function (tel){
                        
                        console.log(tel.get('ddd') + ' ' + tel.get('numero'));
    
    
                        var p = tel.getPessoa();
    
    
                        console.log('Numero pertence a ' + p.get('nome'));
                    });
    
    
                    var end = pessoa.getEndereco();
    
    
                    console.log(end.get('logradouro') + ' ' + end.get('numero'));
                }
            });
            
        });
    Last edited by newuser7000; 26 May 2012 at 10:30 AM. Reason: error code

Thread Participants: 2