About ManyToMany Template

Posts   
 
    
xc_lw2000
User
Posts: 48
Joined: 12-Dec-2006
# Posted on: 08-Dec-2011 14:09:11   

I'm writing a customer template (V3.0 Adapter) I have three entity

UserEntity(include field Id)

UserRoleEntity(include field UserId,RoleId)

RoleEntity(include field Id)

UserEntity and RoleEntity is ManyToMany, and UserRoleEntity as an intermediate entity

I want generate code like this:


public class UserEntity()
{
     // some other code
    RoleFields.Id
}

public class RoleEntity()
{
   // some other code
   UserFields.Id
}

This means that in one class I want know the related ManyToMany entity's PK field. I tried all day,but I can only get such as UserId, RoleId of UserRoleEntity, I can not get all three entity's field info in only one entity class.

How to achieve this,please help me

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-Dec-2011 20:30:55   
  • How are you doing it? and What you have so far?
  • Are you using TDL or LPT?
  • Did you check some built-in templates to see if they do something like you need?

In general, if you want flexibility it's better to use the lpt system, instead of TDL.

David Elizondo | LLBLGen Support Team
xc_lw2000
User
Posts: 48
Joined: 12-Dec-2006
# Posted on: 09-Dec-2011 02:46:55   

In UserEntity class,I have a method to fetch UserEntity collection, and the parameter is a RoleEntity named relatedEntity, So the filter should be RoleEntityFields.Id == relatedEntity.Id,I can generate "RoleEntityFields" string by <[RelatedEntityName]>Fields,but I cant not generate the "Id" string which is RelatedEntity's PK Field Name


public <[If UsePartialClasses]>partial <[EndIf]>class <[CurrentEntityName]>List_Factory
{
<[Foreach RelatedEntity ManyToMany]><[If Not MappedFieldRelationIsHidden]>
        public EntitiyCollection<<[CurrentEntityName]>> Fetch(<[RelatedEntityName]>Entity relatedEntity)
        {
            var llblCollection = new EntitiyCollection<<[CurrentEntityName]>>();
            IRelationPredicateBucket filterBucket = new RelationPredicateBucket();

//confused in this row //RoleEntityFields.Id == relatedEntity.Id //the <[RelatedEntityPKFieldName]> is my imagination, may not be a correct syntax


            filterBucket.PredicateExpression.Add(<[RelatedEntityName]>Fields.<[RelatedEntityPKFieldName]> == relatedEntity.<[RelatedEntityPKFieldName]>);


            filterBucket.Relations.Add(<[CurrentEntityName]>Entity.Relations.<[IntermediateEntityName]>EntityUsing<[Foreach RelationField OneToMany]><[RelatedEntityRelationFieldName]><[NextForeach]>);
            filterBucket.Relations.Add(<[IntermediateEntityName]>Entity.Relations.<[RelatedEntityName]>EntityUsing<[Foreach RelationField ManyToOne]><[RelationFieldName]><[NextForeach]>);
            
            DataProvider.FetchEntityCollection(llblCollection, filterBucket);
            return llblCollection;
        }
<[EndIf]><[NextForeach]>
}

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 09-Dec-2011 07:13:36   

As a relation could have multiple field pairs, I think you should iterate them and then use RelatedEntityPrimaryKeyFieldName. This is an aprox. un-tested code:

public <[If UsePartialClasses]>partial <[EndIf]>class <[CurrentEntityName]>List_Factory
{
<[Foreach RelatedEntity ManyToMany]><[If Not MappedFieldRelationIsHidden]>
        public EntitiyCollection<<[CurrentEntityName]>> Fetch(<[RelatedEntityName]>Entity relatedEntity)
        {
            var llblCollection = new EntitiyCollection<<[CurrentEntityName]>>();
            IRelationPredicateBucket filterBucket = new RelationPredicateBucket();

            <[Foreach RelatedEntityPrimaryKeyField]>
            filterBucket.PredicateExpression.Add(<[RelatedEntityName]>Fields.<[RelatedEntityPrimaryKeyFieldName]> == relatedEntity.<[RelatedEntityPrimaryKeyFieldName]>);
            <[NextForeach]>

             //...

You can find the definition of these tokens in the SDK docs.

David Elizondo | LLBLGen Support Team
xc_lw2000
User
Posts: 48
Joined: 12-Dec-2006
# Posted on: 09-Dec-2011 10:40:07   

daelmo wrote:

As a relation could have multiple field pairs, I think you should iterate them and then use RelatedEntityPrimaryKeyFieldName. This is an aprox. un-tested code:

public <[If UsePartialClasses]>partial <[EndIf]>class <[CurrentEntityName]>List_Factory
{
<[Foreach RelatedEntity ManyToMany]><[If Not MappedFieldRelationIsHidden]>
        public EntitiyCollection<<[CurrentEntityName]>> Fetch(<[RelatedEntityName]>Entity relatedEntity)
        {
            var llblCollection = new EntitiyCollection<<[CurrentEntityName]>>();
            IRelationPredicateBucket filterBucket = new RelationPredicateBucket();

            <[Foreach RelatedEntityPrimaryKeyField]>
            filterBucket.PredicateExpression.Add(<[RelatedEntityName]>Fields.<[RelatedEntityPrimaryKeyFieldName]> == relatedEntity.<[RelatedEntityPrimaryKeyFieldName]>);
            <[NextForeach]>

             //...

You can find the definition of these tokens in the SDK docs.

Yes,It's worked. Thank you very much