Forums

Read data - one function to retrieve 0, 1 or all records?

2D77rus 25 Jun, 2019
Hi!
Maybe it's a simple question, but I'm stuck on it.
Given a table with primary autoinc key on "id" field, and a Read data function set up with primary model on this table. I want this function to retrieve a) all records when {data:id} is not defined; b) one record when {data:id} is defined; c) empty set when {data:id} is defined and equals 0.
Tried "where conditions" id:{data:id}, id:{data:id}/-, id:{data:id/0} and so on. Also tried id:{var:id} combinations, giving {fn:read1$id=(data:id)}. Some fail to get all records, some fail to get an empty set. Am I missing a simple way to get wanted behavior?
Thanks!
healyhatman 26 Jun, 2019
Have the read data set to "All matching"

A) Set the 'on null value' (rightmost dropdown) to 'continue'
B) If ID is defined, use {var:read_dataname.0} to get the first element in the set, which in this case should be the only element in the set.
C) It SHOULD give you an empty array, unless of course you have an ID of 0 in your table.

If you use {data:id/0} then you are saying "if there's no ID, use 0 as the ID" which means you'll get nothing.
/- and /+ are no longer used

Also {fn:read1$id=(data:id)} is unnecessary - just use {data:id} in the read data in that case no need to explicitly pass it, AND passed parameters like that are accessed with {var
{fn:read1$foo=(data:bar)}
In this case, inside read1 you would use {var:foo} (I mean, OR just {data:bar} passing parameters like this is more useful in table list views and loops)
2D77rus 26 Jun, 2019
Thank you. It SHOULD, but it doesn't.

If I set 'on null value' to 'use' then value ==0 returns an empty array, but the same on an undefined {data:id} .
But if I set 'on null value' to 'continue' then value ==0 is also treated as null value, all records are returned. (Debug log shows that this rule is omitted)

So for now I've created a switch function which calls read function only if {data:id} != 0. Ugly but works.
This topic is locked and no more replies can be posted.