0

I hope that I may ask this, but is there a straight forward code example for a subform to save the data and to retrieve the data. What I have now is written an save function in controller/model with an insert statement to save the data and I'm thinking what is the best way to populate the edit.php with data. I can write an select query to get the data but I think this is not the way. And I'm not the only one who strugling with this I think.

BR.

Nico

  • 1
    You haven't really given us much context to work with. If you have a broken coding attempt, please show us that and any errors that it is generating. If you have researched, but that has only confused you, please show us links to the confusing resources. This question is too incomplete and it will either attract vague answers or no answers at all. I am inclined to close this question as Unclear - please edit. – mickmackusa Jun 26 at 0:52
  • Ok, I'm busy with it maybe I can ask this: for the subform do I have to write my own public save funcition in the controller/model? Y/N. If No, than I expect that Joomla this is doing which means for me: saving, removing, updating 'repeatable' records in the database! At this moment a record is created, the id is incremented, but the data is not saved. So is this due that I have to write my own save controller/model function or not? So therefore I would like to see a simple straightforward example with two or three fields which shows how todo this. I see with google I'm not the only one! – user2363969 Jun 27 at 14:50
  • We need more context. Please set up a mcve. Writing up a complete, lengthy, and general-use tutorial/walkthrough is not typically what Stack Exchange hopes to require of an answer. Please make your question as attractive as possible by isolating your problem to specific operation in your application and ask as little as possible from volunteers. – mickmackusa Jun 27 at 19:06
1

Subform data is passed as an array. To store it in database you have to convert it to a string such as JSON. How and where to do this depends on how your component is written. But if your component uses Table API, you can add support for JSON encoded fields by declaring $_jsonEncode class property in the table class:

class MyTable extends JTable
{
    protected $_jsonEncode = ['subform_field', 'another_subform_field'];
}
| improve this answer | |
0

I have it now working for a week. Someone give me an example and it is simpler than I thought! And I was on the wrong track.

I read on some forums about a heading- and a Child table. My mindset about a heading- and child table that these are two 'physical' different tables related to each other by a foreign key. If I understand now, this is not the idea behind it. It is one table with 'main' data and a field with 'child' data saved as JSON format.

Than an second important thing the field in that table MUST have the SAME name as the subform name!! Otherwise the data is not saved and you don't get an error message!

The subform is good to use for let say a floor (heading) - room (child) relation. I thing it is hard to use if you have table without a heading-child relation, but just simple config-data. I thought it is nice to use in this case as well because you don't have to fill in one value and press every time 'save-new'. With a subform it is more user friendly IMHO, you press the '+' sign and add a value.

In the 'example' I received the function check() ('tables'.php) added with the line:

function check() {
    //You can add here more as filling created_by etc.
    $this->subformname = json_encode($this->subformname);
}

Were the function check come from, I don't know, but works

And at the edit.php you can simply add:

echo $this->form->renderField('subformname');

I hope that I have not forgotten something but than it works.

Except of the edit.php line, I was not AWARE of this these things and I could not find something about these specific issue's! I was on a way to make my own 'save' function for the controller and model. But after some hours in some days, I thought this can't be idea behind it, because it is a joomla field so it should more or less work out of the box, but how does this work!???

Therefore it should be nice if there is simple straightforward tutorial or example, so that someone can see that it works or at least it gives you the idea that it works! further I don't ask anything from anyone.

I'm not asking from you or anyone else to make a specific example or tutorial for me but maybe you know were I can find this important example/tutorial. And I don't know were mickmackusa has found these fundamental information, but if you can point me to this, than I feel bad.

And if you can point me to this information maybe you can point me also to the info were is explained what the functionality is and when should use it about:

  • function postSaveHook
  • function postDeleteHook
  • function validate
  • and maybe function check (this was new for me)

And otherwise, I hope that I will come along this information while I'm searching for something else.

BR.,

Nico

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.