how to check if relation field is nullable in TDL?

Posts   
 
    
Posts: 8
Joined: 29-Jun-2009
# Posted on: 01-Jul-2009 15:42:44   

I've got TDL code like this:

<[ Foreach RelatedEntity ManyToOne ]> <[ Foreach RelationField ]> ... <[ NextForeach ]> <[ NextForeach ]>

Now, inside those loops, how do I check nullability and .net type of relation field on current entity's side? Is there a way to do this in TDL?

Walaa avatar
Walaa
Support Team
Posts: 14987
Joined: 21-Aug-2005
# Posted on: 01-Jul-2009 15:59:50   

What do you mean by the nullability and .net type of relation field?

Are you speaking about the FK field or the field mapped to the relation? For example: Order.CustomerId or Order.Customer?

Posts: 8
Joined: 29-Jun-2009
# Posted on: 01-Jul-2009 16:04:41   

I mean FK field, that is Order.IdCustomer in your example.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 01-Jul-2009 21:40:22   

Use this

<[If _Not _IsNullable]> ... <[EndIf]>

Ref:

Will handle text further further if the current entity field / field mapped on related field is nullable in the DB (i.e.: the target field is nullable), OR if the current stored procedure parameter is marked nullable.

Or maybe this:

<[If _Not _GenerateAsNullableType]> ... <[EndIf]>

Ref:

Will handle text further if the current entity field / field mapped on related field, should be generated as a Nullable(Of T) typed field. This is true if the field is nullable AND the field is a value type AND the field's property GenerateAsNullableOfT is set to true. Use in .NET 2.0. Not used for stored procedure parameters, these are always generated as nullable types, if applicable (valuetype)

For more info, read the SDK manual.

David Elizondo | LLBLGen Support Team
Posts: 8
Joined: 29-Jun-2009
# Posted on: 02-Jul-2009 08:22:20   

I'm aware of those constructs, but they don't seem to work in this case. When I write this:

<[ Foreach RelatedEntity ManyToOne ]> <[ Foreach RelationField ]> <[ If IsNullable ]> nullable <[ Else ]> not nullable <[ EndIf ]> <[ NextForeach ]> <[ NextForeach ]>

The output for each field is:

nullable

not nullable

Both texts are in the output. The <[ If IsNullable ]> statement works perfectly inside <[ ForEach EntityField ]> loop. The same is for <[ If GenerateAsNullableType ]> statement.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39771
Joined: 17-Aug-2003
# Posted on: 03-Jul-2009 11:15:23   

(I assume you're using v2.6 latest build, you didn't mention that)

IsNullable works on the current entity field. Foreach relationfield sets the currententityfieldrelation member for the current scope, not the current entity field. I.o.w.: the If statement works on an element which doesn't change.

What you should do is simply traverse over all the fields using foreach entityfield and then use inside that loop the statement If IsForeignKey, which results in false if the field isn't an FK field.

Keep in mind that TDL is a limited language with statements which are enough to generate our generated code. That doesn't mean it's enough to generate YOUR generated code. We therefore recommend users to use .lpt templates instead, which are includable into TDL templates if you want to.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 8
Joined: 29-Jun-2009
# Posted on: 03-Jul-2009 14:56:44   

Otis, thanks for your reply. I've come up with solution involving an included lpt template inside tdl template. I was just wondering if this was possible in clear TDL.