Populating the parent key in the child
Populating the parent key in the child
cypressit
Posts: 4Questions: 1Answers: 0
I have a parent table (invoices) and a child table (details). I am using the editor to work the parent child example I found here on this site. My issues is that I must manually populate the invoice_id filed in the child record when I use the "new" button. Is there a way to have this be automatically filled in when the form opens?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Consider this:
1. JS - with Ajax you pass the id of the selected row of the parent table on to the PHP Editor instance of the child table:
Is it this you are using? Parent / child with Editor?
The parent id should be set by the call to
field().def()
for new records.Allan
Why wouldn't you recommend my way of doing it on the server side? Under normal circumstances you don't really want to see the artificial auto-increment id of a parent row on the screen anyway ...
I was keeping it as part of the Editor form submit as it means its easy to change the value (either on create or edit).
Doing it server-side with the extra parameter as you have is perfectly valid as well. A little extra code on the server-side, rather than a little extra on the client .
Allan
I had tried that I thought. When the new form opens, I have to manually populate the invoice_id. Here is my code:
Wow, this is the first time I tried to post code. How do I format it so you can read it?
Use Markdown, see below. Triple back ticks to highlight code.
You can also drop the invoice id from the form of the child table and set it on the server side as per my example above. If it is not in the child table form you cannot be asked to enter it Allan confirmed that both ways (client or server side) are acceptable.
I've edited the post to use Markdown to make the code readable.
I don't see any code in the above that will set the default for the invoice_id.
This is the part of the blog post that is relevant for that.
Allan
Thanks! That one line change made all the difference!