DMXzone App Connect Support Product Page

Answered

Creating a record in a second table from query results from another table

Asked 24 May 2017 22:10:45
1
has this question
24 May 2017 22:10:45 Tom Dupre posted:
(I don't know if this would be better in the Server Connect or App Connect sections - it probably belongs to both.)

I would like to display a list of search results from a paged query and include a button or link in each row to create a new record in another table (including the id from the selected record and the id of the logged-on user).

I suppose I could put pass the selected record id to a second page with hidden form fields, and add another button. I expect I could do this with a cookie or session, though I am having trouble understanding how to access or display {{SecurityProviderId}}/ identity or whatever it should be. I can see the session with the user id is being created following a successful login.

I would prefer not to use a second page, so I tried incorporating a form in each returned row but can't work out how to pick up the form values. I can create new records with static values, but not with dynamic ones.

There are some basic issues I don't yet understand. Eg I am not sure if I should create two server action files - one for the query displaying the search results and another for the insert record. Assuming there should just be one file, do I just need three steps: Connection, Query and Insert or should I add a repeater in some way (repeaters in Server Connect seem to have some useful buy mysterious uses)? Given the search results are displaying correctly, I don't know why I can't just use dmx-bind:value="{{myField}}, given that {{myField}} displays the data, but I think some other steps must be needed. I've tried adding a second Repeat in App Connect, but I'm just guessing really.

I would be grateful for any specific help - or just pointing in the right direction. Perhaps there is some way of doing an insert from a link rather than a form.

Thanks in advance.

Replies

Replied 25 May 2017 10:02:48
25 May 2017 10:02:48 Teodor Kuduschiev replied:
Hi Tom,
It is really easy to do this with app connect and server connect.

In server connect:
You need two server actions - one to display the products and another for insert.
For the insert action, you need to setup a get variable (let's call it id). Then, use this get variable in the insert step, as a value.

In app connect:
Add two server connect components - one for the data and one for the insert.
Create your repeat region with data.
Add the button you want to use, to insert data into the database in your repeat region. Click the button, then add dynamic event > mouse > click and select the insert server action > load.
There you will see its get var. Just select a dynamic value for it - the data picker dialog will appear and click the binding you want to insert.

Replied 25 May 2017 20:55:44
25 May 2017 20:55:44 Tom Dupre replied:
Thanks Teodor,

That was very helpful. Creating a record in the second table in this way - using a separate server action file - is simpler than the way I was doing it and makes more sense. However I still haven't quite managed to pass the correct data into inserted records.

I created a GET variable in the insert action file as you suggested and selected this as the insert value - {{$_GET.id}}.

In App Connect I added the two Server Connect components, and a Repeat to display the query results. I clicked 'App' above the list of component, then Define Query Params - and added 'id' as a parameter. I added a Dynamic Event to the button, selected the insert action and chose the id input parameter. I tried the button at this point; the insert worked, but no data was included.

I then selected the insert Server Connect component and added an Input Parameter: the field containing the id value, from the Repeat component. This looked promising, but again no data was entered into the inserted record. The input parameter looked like this: repeat1[0].stock_id.

For the button's Dynamic Event, I then tried choosing the id from the main Server Connect component (the one displaying the records). This actually inserted an id value into the new record - but only the value from the first record, whichever button I clicked. This is not surprising, given the [0] in the selected parameter, which looked like this:
serverconnect1.data.query1.data[0].stock_id.

The issue seems to be getting the id from the repeat, not the record with the index of [0].

I expect the solution is just a matter of selecting another option at some point - and is, as you say really easy - but until then, it's really hard.

I would be grateful for your help.
Replied 26 May 2017 08:17:19
26 May 2017 08:17:19 Teodor Kuduschiev replied:
Hi Tom, you should not define Query Param ... just click the button, inside the repeat, select dynamic events > server action > load and you will see the GET variable, created in the server action there. Then just bind the value from the repeat for it.
Just don't do anything different than what i described in my previous message:

Quote
In app connect:
Add two server connect components - one for the data and one for the insert.
Create your repeat region with data.
Add the button you want to use, to insert data into the database in your repeat region. Click the button, then add dynamic event > mouse > click and select the insert server action > load.
There you will see its get var. Just select a dynamic value for it - the data picker dialog will appear and click the binding you want to insert.

Replied 26 May 2017 11:46:27
26 May 2017 11:46:27 Tom Dupre replied:
Hi Teodor,

Great - it's working now. There was one step I misunderstood. As you say, it is easy (when you know how).

I also need to insert the id of the logged-on user but haven't managed to access it. I know the session is set because this:
<?php echo isset($_SESSION['dbSecurityId'])? $_SESSION['dbSecurityId']:''; ?>
.. returns the user id.

How do I access this with App Connect? I've tried things like:
{{dbSecurityId}}, {{serverconnect1.data.identity.identity}}, {{serverconnect1.data.identity}}
.. but nothing is displayed. Apart from inserting into a record, I also need it for queries, conditionally redirecting or displaying page sections etc.. I am quite sure this is another easy one - but I haven't found it yet.

Finally, if a user has to be logged in to insert a record, is the best approach to add a Validate Data step in Server Connect and add a static event (on error) in App Connect to redirect to a login page ?

Many thanks for your help,
Tom
Replied 29 May 2017 07:16:44
29 May 2017 07:16:44 Teodor Kuduschiev replied:
Hi Tom,
In order to be able to insert the logged in user id you just have to define the session variable in the Globals > Sessions. Just use your session name, in your case: dbSecurityId
Then you will be able to select it in the insert step, from the dynamic data picker.
Replied 30 May 2017 09:12:53
30 May 2017 09:12:53 Tom Dupre replied:
Hi Teodor,

Thanks. That was helpful - I can now include the id stored in the session variable in the insert action.

I still haven't been able to find out how to access the id - eg to display it on a page or, more importantly, to determine if a user is logged on so I can display parts of a page conditionally.

I know the session is available - I can display it using PHP. Applying Security Provider Enforcer seems to make the session available. Is there something else I need to do to make it available dmx-show/hide or dmx-bind:condition? It is available as in input parameter for the insert action.

Reply to this topic