subsonic / SubSonic-3.0

SubSonic 3.0 for the .NET 3.5 Framework
http://subsonic.github.io/
557 stars 209 forks source link

Self referencing table (ID, ParentID) #248

Open onyx84 opened 13 years ago

onyx84 commented 13 years ago

I am using ActiveRecord

I have a company table with fields:

Id | ParentId | CompanyName | Description ParentId referencing to Id, which creates a parent child relationship.

the generated Code is incorrect?

from T4 template ActiveRecord.tt

    public IQueryable<<#=fk.OtherClass #>> <#=propName #>
    {
        get
        {

              var repo=<#=Namespace #>.<#=fk.OtherClass#>.GetRepo();
              return from items in repo.GetAll()
                   where items.<#=CleanUp(fk.OtherColumn)#> == _<#=CleanUp(fk.ThisColumn)#>
                   select items;
        }
    }

will generate :

    public IQueryable<Company> Companies
    {
        get
        {

              var repo=EMS.Data.Company.GetRepo();
              return from items in repo.GetAll()
                   where items.Id == _ParentId
                   select items;
        }
    }

I added Checking if the table is referencing itself:

    public IQueryable<<#=fk.OtherClass #>> <#=propName #>
    {
        get
        {

              var repo=<#=Namespace #>.<#=fk.OtherClass#>.GetRepo();
              return from items in repo.GetAll()
                   <# if(fk.OtherClass != tbl.ClassName) { #>
                   where items.<#=CleanUp(fk.OtherColumn)#> == _<#=CleanUp(fk.ThisColumn)#>
                   <#}else {#>
                   where items.<#=CleanUp(fk.ThisColumn)#> == _<#=CleanUp(fk.OtherColumn)#>
                   <#}#>
                   select items;
        }
    }

which generates this code:

    public IQueryable<Company> Companies
    {
        get
        {

              var repo=EMS.Data.Company.GetRepo();
              return from items in repo.GetAll()
                                          where items.ParentId == _Id
                                          select items;
        }
    }

which i think is correct since Companies represent the Child company of my entity.