Accessing RelatedEntityFields in TDL

Posts   
 
    
tomahawk
User
Posts: 169
Joined: 02-Mar-2005
# Posted on: 15-Jan-2010 02:16:21   

Hi there,

I have a custom template and I'm looping through Many To One RelatedEntities but can't figure out how to access the FKey field of the currentEntity that represents the relation.

Here's the entity template code:


<[Foreach RelatedEntity ManyToOne]><[If Not MappedFieldRelationIsHidden]>
        <[RelatedEntityName]> f<[MappedFieldNameRelation]>;
        [Association("<[RelatedMappedFieldNameRelation]>-<[RelatedEntityName]>-<[CurrentEntityName]>")]
        [Persistent("<[RelatedEntityFieldName]>")] // I need the fkey Field in this entity which represents the current relation here
        public <[RelatedEntityName]> <[MappedFieldNameRelation]>
        {
            get { return f<[MappedFieldNameRelation]>; }
            set { SetPropertyValue<<[RelatedEntityName]>>("<[MappedFieldNameRelation]>", ref f<[MappedFieldNameRelation]>, value); }
        }<[EndIf]><[NextForeach]>

Example code of what I need:

public ContactEntity
{
   public int cTypeID; // fkey to CType table

// relation property
   [Persistent("cTypeID")]  // can't get 'cTypeID' when looping through related entities
   public CType ContactType;
}

Using Template Studio: 2.6.06102008 Final

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 15-Jan-2010 02:36:01   

A relation can have one or more fields. So you should try this:

<[Foreach RelatedEntity ManyToOne]><[If Not MappedFieldRelationIsHidden]>
        <[RelatedEntityName]> f<[MappedFieldNameRelation]>;
        [Association("<[RelatedMappedFieldNameRelation]>-<[RelatedEntityName]>-<[CurrentEntityName]>")]

       <[Foreach RelationField]>
        [Persistent("<[RelationFieldName]>")] 
       <[NextForeach]>

        public <[RelatedEntityName]> <[MappedFieldNameRelation]>
        {
            get { return f<[MappedFieldNameRelation]>; }
            set { SetPropertyValue<<[RelatedEntityName]>>("<[MappedFieldNameRelation]>", ref f<[MappedFieldNameRelation]>, value); }
        }<[EndIf]><[NextForeach]>
David Elizondo | LLBLGen Support Team
tomahawk
User
Posts: 169
Joined: 02-Mar-2005
# Posted on: 15-Jan-2010 02:41:42   

Thanks, just what I needed!

tomahawk
User
Posts: 169
Joined: 02-Mar-2005
# Posted on: 06-May-2010 23:40:11   

I am using the following in my template.


<[Foreach RelatedEntity OneToMany]>[Include]
[Association("<[ MappedFieldNameRelation ]>", "<[Foreach PrimaryKeyEntityField Comma]><[ EntityFieldName ]><[NextForeach]>", "<[Foreach RelationField]><[If IsForeignKey]><[RelatedEntityRelationFieldName]><[EndIf]><[NextForeach]>", IsForeignKey=false)]
public IEnumerable<<[ RelatedEntityName ]>Entity> <[ MappedFieldNameRelation ]>;
<[ NextForeach ]>

In the place where I am referencing the 'PrimaryKeyEntityField' I actually need the foreign key fields, not the primary keys of the entity. How can I get access to those?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-May-2010 06:24:05   

Please show us the expected code snippet.

David Elizondo | LLBLGen Support Team
tomahawk
User
Posts: 169
Joined: 02-Mar-2005
# Posted on: 07-May-2010 09:07:33   

ok, entities: ContactAddress: pkey is 2 columns:ContactID, AddressID : AddressID is foreign key to... **Address **: pkey is AdrID

I want to see Association("ContactAddress", "AddressID", "AdrID")

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 07-May-2010 10:38:33   

Instead of Foreach PrimaryKeyEntityField, Use Foreach RelationField. And the FKfield Token is [RelationFieldName]

Usage example from dao.template:

private IPredicateExpression CreateFilterUsingForeignKeys(<[Foreach RelatedEntity ManyToOne IncludeInherited Comma]>IEntity <[CaseCamel MappedFieldNameRelation]>Instance<[NextForeach]>, IEntityFields fieldsToReturn) { IPredicateExpression selectFilter = new PredicateExpression(); <[Foreach RelatedEntity ManyToOne IncludeInherited]> if(<[CaseCamel MappedFieldNameRelation]>Instance != null) { <[Foreach RelationField CrLf]>selectFilter.Add(new FieldCompareValuePredicate(fieldsToReturn[(int)<[CurrentEntityName]>FieldIndex.<[RelationFieldName]>], ComparisonOperator.Equal, ((<[ RelatedEntityName ]>Entity)<[CaseCamel MappedFieldNameRelation]>Instance).<[RelatedEntityRelationFieldName]>));<[NextForeach]> }<[NextForeach]> return selectFilter; }

tomahawk
User
Posts: 169
Joined: 02-Mar-2005
# Posted on: 07-May-2010 11:10:52   

thanks for the help