2

I'm developing my first Joomla component and it has been very hard to find proper documentation, so basically I've been checking core components to see how they get things done and try to apply the same method in my component.

However I cannot find how to save related tables. For example, let's say I have three tables:

Users

id,username, other_relevant_columns

Roles

id,rolename, other_relevant_columns

UsersRoles

user_id, role_id other_relevant_columns

When I create a new user, I need to specify which roles the user has and basically I have no idea where to add this code. Do I need to create a custom save method in order to achieve this? Where?

Also, how to display this information? I would like to display a group of checkboxes with all the roles that an user can have but I guess I cannot achieve this using the xml file to generate forms (because I need to load those values from the database and also need to retrieve the values in order to check/uncheck the checkboxes)

PS: Keep in mind that these are only example tables. I'm not actually implementing a user-roles functionality.

2
  1. Yep, you will need a custom save method to handle the pivot tables. Probably on on delete event too to handle the removal of records etc.

  2. Custom form fields (which can use db sourced options) are pretty easy, the doc is here: https://docs.joomla.org/Creating_a_custom_form_field_type

1

When you create your main table ("Users" in your example above), then you need, as given in the "Create your own component" docs, a corresponding table object and a corresponding model object.

Extending the correct core model will give you the basics of what you want.

You've got linked tables, so your "main" model will not only need to do its own work (save, in the example you've given), but also action the sub-tables as required, so you override the save method in this main model.

You could just do all the work, for the sub tables too, in the main object methods, but it would be smarter to create table and model objects for the sub-tables too and trigger their relevant methods from the main model.

As james points out, you'll probably want to do the same with other methods too, delete &tc

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.