One to many in a calculation

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

One to many in a calculation

Postby d.kull » Tue Nov 22, 2016 5:35 pm

I have a one-to-many relation. The goal is to display all matched dataproviders in a calculation as a string.

So far I used the following code which works.

Code: Select all

function string_joint()
{
   var name = databaseManager.getFoundSetDataProviderAsArray(person_root_to_pers_name,'d_pers_name');
   var j_names = name.join(' ')
   return j_names   
}


But it looks that this is not the way to be supposed to do with servoy 8.1 Can someone give me a light how this can be achieved ? I've tried to do it by using an array and join it. But I receive only one matching field and not many since there were some.
I use this function for displaying the first name and the surname of a person. One table holds the "identity' of the person and the other table the name data.

Thanks for helping me.
'
d.kull
 
Posts: 10
Joined: Mon Mar 21, 2016 3:13 pm

Re: One to many in a calculation

Postby patrick » Wed Nov 23, 2016 11:42 am

I think that method has long been deprecated. You can loop over your related foundset instead, e.g.

Code: Select all
function string_joint()
{
   var names = [];
   for ( var i = 1; i <= person_root_to_pers_name.getSize() ; i ++) {
      names.push(person_root_to_pers_name.getRecord(i).d_pers_name);
   }
   return names.join(' ');
}
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Re: One to many in a calculation

Postby d.kull » Wed Nov 23, 2016 12:39 pm

That works fine. So many thank's for your help.

Dominique Kull
d.kull
 
Posts: 10
Joined: Mon Mar 21, 2016 3:13 pm

Re: One to many in a calculation

Postby Bernd.N » Thu Nov 24, 2016 2:13 pm

As you have the separator defined as blank, you need to rely on the fact that all names have no blanks inside, I guess, which might be not the case always.

And when you use that function as a table-based-calculation, it might be better to externalize it if possible, as the operation is a bit costly due to database operations and you can not really control when a table-based-calculation is called.
Bernd Korthaus
LinkedIn
Servoy 7.4.9 SC postgreSQL 9.4.11 Windows 10 Pro
User avatar
Bernd.N
 
Posts: 544
Joined: Mon Oct 21, 2013 5:57 pm
Location: Langenhorn, North Friesland, Germany

Re: One to many in a calculation

Postby mboegem » Thu Nov 24, 2016 3:11 pm

Bernd.N wrote:As you have the separator defined as blank, you need to rely on the fact that all names have no blanks inside, I guess, which might be not the case always.

It's a join, not a split... so this will never be an issue.

patrick wrote:I think that method has long been deprecated.

Easy replace is this:
Code: Select all
function string_joint()
{
   var name = databaseManager.convertToDataSet(person_root_to_pers_name,['d_pers_name']).getColumnAsArray(1);
   var j_names = name.join(' ');
   return j_names;
}


EDIT: sorry... just noticed you're referring to calculations here
databaseManager functions are no longer available in calculation-mode since some versions.
So the loop would be your only option, however: using relations in calculations is not recommended as it can slowdown your application big time.
Marc Boegem
Solutiative / JBS Group, Partner
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image

Partner of Tower - The most powerful Git client for Mac and Windows
User avatar
mboegem
 
Posts: 1743
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: One to many in a calculation

Postby Bernd.N » Thu Nov 24, 2016 4:45 pm

>> It's a join, not a split... so this will never be an issue.

Yes I know, but the resulting string will have some meaning for some other function usually, which will assume that all strings that are separated by a blank are separate names.
So Mr. Doe Meyers will result in two persons for any following functions.
Bernd Korthaus
LinkedIn
Servoy 7.4.9 SC postgreSQL 9.4.11 Windows 10 Pro
User avatar
Bernd.N
 
Posts: 544
Joined: Mon Oct 21, 2013 5:57 pm
Location: Langenhorn, North Friesland, Germany

Re: One to many in a calculation

Postby rgansevles » Sat Nov 26, 2016 6:04 pm

Note that Servoy will check which data is touched in your calculation.
When calling databaseManager.getFoundSetDataProviderAsArray Servoy does not detect you are using the d_pers_name fields, so the calculation will not refresh on changes.

When you use Patrick's loop, it will detect changes on the values and the calculation will automatically update.

Rob
Rob Gansevles
Servoy
User avatar
rgansevles
 
Posts: 1927
Joined: Wed Nov 15, 2006 6:17 pm
Location: Amersfoort, NL


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 11 guests

cron