Model created by StoreFor<> and multi HasManayAssociation Attribute

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Model created by StoreFor<> and multi HasManayAssociation Attribute

    I have a problem with HasManyAssociation Attribute.

    My sample has 7 classes, having associations with each other.

    School HasMany ClassRoom and Club

    ClassRoom HasMany Lecture and Facility

    Club HasMany Event and ClubLecture


    It's part of my sample...

    -- School Class
    This class has many ClassRooms and Clubs classes
    namespace Test.Models
    {
        using Ext.Net.MVC;
        using System;
        using System.Collections.Generic;
    
        [HasManyAssociation(Model = "Test.school.ClassRoom", Name = "classRooms", PrimaryKey = "Id", AssociationKey = "ClassRooms", ForeignKey = "SchoolId")]
        [HasManyAssociation(Model = "Test.school.Club", Name = "clubs", PrimaryKey = "Id", AssociationKey = "Clubs", ForeignKey = "SchoolId")]
        [Model(Name = "Test.school.School")]
        public partial class School
        {
            public School()
            {
                this.ClassRooms = new HashSet<ClassRoom>();
                this.Clubs = new HashSet<Club>();
            }
    
            [ModelField(IDProperty = true)]
            public int Id { get; set; }
            public string Name { get; set; }
            public string Location { get; set; }
    
            [ModelField(Ignore = true)]
            public virtual ICollection<ClassRoom> ClassRooms { get; set; }
            public virtual ICollection<Club> Clubs { get; set; }
        }
    }
    -- ClassRoom Class
    This Class belongs to School class
    and has many Lectures and Facilities classes
    namespace Test.Models
    {
        using Ext.Net.MVC;
        using System;
        using System.Collections.Generic;
    
        [HasManyAssociation(Model = "Test.school.Lecture", Name = "lectures", PrimaryKey = "Id", AssociationKey = "Lectures", ForeignKey = "ClassRoomId")]
        [HasManyAssociation(Model = "Test.school.Facility", Name = "facilities", PrimaryKey = "Id", AssociationKey = "Facilities", ForeignKey = "ClassRoomId")]
        [BelongsToAssociation(Model = "Test.school.School")]
        [Model(Name = "Test.school.ClassRoom")]
        public partial class ClassRoom
        {
            public ClassRoom()
            {
            }
    
            [ModelField(IDProperty = true)]
            public int Id { get; set; }
            public int SchoolId { get; set; }
            public string Name { get; set; }
            public string Location { get; set; }
    
            [ModelField(Ignore = true)]
            public virtual School School { get; set; }
        }
    }
    -- Club Class
    This class belongs to School class
    and has many Events and ClubLectures
    namespace Test.Models
    {
        using Ext.Net.MVC;
        using System;
        using System.Collections.Generic;
    
        [HasManyAssociation(Model = "Test.school.ClubLecture", Name = "clubLectures", PrimaryKey = "Id", AssociationKey = "ClubLectures", ForeignKey = "ClubId"),
        HasManyAssociation(Model = "Test.school.Event", Name = "events", PrimaryKey = "Id", AssociationKey = "Events", ForeignKey = "ClubId")]
        [BelongsToAssociation(Model = "Test.school.School")]
        [Model(Name = "Test.school.Club")]
        public partial class Club
        {
            public Club()
            {
                this.ClubLectures = new HashSet<ClubLecture>();
                this.Events = new HashSet<Event>();
            }
    
            [ModelField(IDProperty = true)]
            public int Id { get; set; }
            public int SchoolId { get; set; }
            public string Name { get; set; }
    
            [ModelField(Ignore = true)]
            public virtual School School { get; set; }
            [ModelField(Ignore = true)]
            public virtual ICollection<ClubLecture> ClubLectures { get; set; }
            [ModelField(Ignore = true)]
            public virtual ICollection<Event> Events { get; set; }
        }
    }
    -- Facility Class
    This class belongs to ClassRoom class
    namespace Test.Models
    {
        using Ext.Net.MVC;
        using System;
        using System.Collections.Generic;
    
        [BelongsToAssociation(Model = "Test.school.ClassRoom")]
        [Model(Name = "Test.school.Facility")]
        public partial class Facility
        {
            [ModelField(IDProperty = true)]
            public int Id { get; set; }
            public int ClassRoomId { get; set; }
            public string Name { get; set; }
            public string Purpose { get; set; }
    
            [ModelField(Ignore = true)]
            public virtual ClassRoom ClassRoom { get; set; }
        }
    }
    -- Lecture Class
    This class belongs to ClassRoom class
    namespace Test.Models
    {
        using Ext.Net.MVC;
        using System;
        using System.Collections.Generic;
    
        [BelongsToAssociation(Model = "Test.school.ClassRoom")]
        [Model(Name = "Test.school.Lecture")]
        public partial class Lecture
        {
            [ModelField(IDProperty = true)]
            public int Id { get; set; }
            public int ClassRoomId { get; set; }
            public string Name { get; set; }
            public string StartTime { get; set; }
            public string EndTime { get; set; }
    
            [ModelField(Ignore = true)]
            public virtual ClassRoom ClassRoom { get; set; }
        }
    }
    -- Event Class
    This class belongs to Club class
    namespace Test.Models
    {
        using Ext.Net.MVC;
        using System;
        using System.Collections.Generic;
    
        [BelongsToAssociation(Model = "Test.school.Club")]
        [Model(Name = "Test.school.Event")]
        public partial class Event
        {
            [ModelField(IDProperty = true)]
            public int Id { get; set; }
            public int ClubId { get; set; }
            public string Name { get; set; }
            public Nullable<System.DateTime> StartDate { get; set; }
            public Nullable<System.DateTime> EndDate { get; set; }
            public string Description { get; set; }
    
            [ModelField(Ignore = true)]
            public virtual Club Club { get; set; }
        }
    }
    -- ClubLecture Class
    This class belongs to Club class
    namespace Test.Models
    {
        using Ext.Net.MVC;
        using System;
        using System.Collections.Generic;
    
        [BelongsToAssociation(Model = "Test.school.Club")]
        [Model(Name = "Test.school.ClubLecture")]
        public partial class ClubLecture
        {
            [ModelField(IDProperty = true)]
            public int Id { get; set; }
            public int ClubId { get; set; }
            public string Name { get; set; }
            public Nullable<System.DateTime> StartDate { get; set; }
            public Nullable<System.DateTime> EndDate { get; set; }
            public Nullable<int> MaxMember { get; set; }
    
            [ModelField(Ignore = true)]
            public virtual Club Club { get; set; }
        }
    }
    -- part of cshtml
    I create client model with StoreFor<>
    
            Html.X().GridPanel()
            .ID("gvSchool")
            .AutoDestroy(true)
            .Width(500)
            .Height(50)
            .Hidden(false)
            .Store(
                Html.X().StoreFor<Test.Models.School>()
                .ID("storeSchool")
                .AutoDestroy(false)
                .Data(Model)
            )
    -- Created Javascript
    only one association defined
            items: [{
                store: {
                    model: Ext.define("Test.school.School", {
                        extend: "Ext.data.Model",
                        ns: "School",
                        fields: [{
                            name: "Id",
                            type: "int"
                        }, {
                            name: "Name",
                            type: "string"
                        }, {
                            name: "Location",
                            type: "string"
                        }, {
                            name: "Capacity",
                            type: "int"
                        }, {
                            name: "Area",
                            type: "float"
                        }, {
                            name: "Clubs"
                        }],
                        idProperty: "Id",
                        associations: [{
                            type: "hasMany",
                            associationKey: "ClassRooms",
                            primaryKey: "Id",
                            model: "Test.school.ClassRoom",
                            foreignKey: "SchoolId",
                            name: "classRooms"
                        }]
                    }),
                    storeId: "storeSchool",
                    ns: "School",
                    autoLoad: true,
                    data: {
                        "ClassRooms": [{
                            "Facilities": [],
                            "Lectures": [],
                            "Id": 1,
                            "SchoolId": 1,
                            "Name": "ClassRoom 1",
                            "Location": "1st floor - 1",
                            "Capacity": 50
                        }, {
                            "Facilities": [],
                            "Lectures": [],
                            "Id": 2,
                            "SchoolId": 1,
                            "Name": "ClassRoom 2",
                            "Location": "1st floor - 2",
                            "Capacity": 50
                        }],
                        "Clubs": [],
                        "Id": 1,
                        "Name": "School 1",
                        "Location": "Seoul",
                        "Capacity": 100,
                        "Area": 200.00
                    },
                    proxy: {
                        type: 'memory'
                    }
                },
                id: "gvSchool",
                ns: "School",
                height: 50,
                width: 500,
                xtype: "grid",
                columns: {
                    ns: "School",
                    items: [{
                        ns: "School",
                        xtype: "rownumberer",
                        align: "center"
                    }, {
                        ns: "School",
                        width: 70,
                        align: "center",
                        dataIndex: "Id",
                        text: "School Id"
                    }]
                }, ......
    -- part of Expected Javascript
    I expected two associations like below
                       associations: [{
                            type: "hasMany",
                            associationKey: "ClassRooms",
                            primaryKey: "Id",
                            model: "Test.school.ClassRoom",
                            foreignKey: "SchoolId",
                            name: "classRooms"
                        }, {
                           type: "hasMany",
                            associationKey: "Club",
                            primaryKey: "Id",
                            model: "Test.school.Club",
                            foreignKey: "SchoolId",
                            name: "clubs"
                        }]

    From definition AbstractAssociationAttribute in AbstractAssociationAttribute.cs file
    AllowMultiple = true

    HasManyAssociationAttribute inherit AbstractAssociationAttribute...

    Anything I missed?

    Thanks in advance ^^.
    Last edited by Daniil; Nov 13, 2013 at 12:45 PM. Reason: Aattribute => Attribute
  2. #2
    Hello!

    Please, wrap you code in CODE tags. Read #3 here: http://forums.ext.net/showthread.php?10205

    I don't quite understand your problem, could you elaborate?
  3. #3

    Sorry ^^ I wraped code with CODE tags

    Quote Originally Posted by Baidaly View Post
    Hello!

    Please, wrap you code in CODE tags. Read #3 here: http://forums.ext.net/showthread.php?10205

    I don't quite understand your problem, could you elaborate?
    It's my first time to write at forum.

    Be nice to newbie, please ^^.
  4. #4
    Thank you! So what is your problem with this code? I don't quite understand.
  5. #5

    My ploblem is...

    Quote Originally Posted by Baidaly View Post
    Thank you! So what is your problem with this code? I don't quite understand.
    The problem is

    in the generated code (below), there is only one association
                idProperty: "Id", 
                associations: [{ 
                     type: "hasMany",
                     associationKey: "ClassRooms",
                     primaryKey: "Id",
                     model: "Test.school.ClassRoom",
                     foreignKey: "SchoolId",
                     name: "classRooms"
                 }]
    but it has two associations, so I expect tow associations like below

    associations: [{
          type: "hasMany",
          associationKey: "ClassRooms",
          primaryKey: "Id",
          model: "Test.school.ClassRoom",
          foreignKey: "SchoolId",
          name: "classRooms"
      }, {
         type: "hasMany",
          associationKey: "Club",
          primaryKey: "Id",
          model: "Test.school.Club",
          foreignKey: "SchoolId",
          name: "clubs"
      }]
    As you know without second association, I can't access store for second associated class. That's my ploblem...

    Thank you ^^.
    Last edited by bobae7; Nov 05, 2013 at 4:45 AM.
  6. #6
    Sorry for the delay.

    I'm not sure that it's possible to use the HasManyAssociation attribute more than once. I'll ask my colleagues about it.

    However, you always should be able explicitly define this associations in the Model. Did you try it? Did it work for you?

Similar Threads

  1. Replies: 1
    Last Post: Jul 30, 2013, 4:36 PM
  2. [CLOSED] How to use StoreFor Model
    By UnifyEducation in forum 2.x Premium Help
    Replies: 3
    Last Post: Oct 18, 2012, 2:09 PM
  3. Replies: 0
    Last Post: Aug 01, 2012, 2:20 AM
  4. Replies: 2
    Last Post: Aug 09, 2011, 11:38 AM
  5. Multi Select - JIT
    By amitpareek in forum Open Discussions
    Replies: 1
    Last Post: Dec 09, 2008, 10:31 AM

Tags for this Thread

Posting Permissions