1

I have a custom database table created with my component that has a user id linked by foreign key to #__usersand I have a form field type in Joomla 3.x mvc

<field name="modified_user_id"
type="user"
label="JGLOBAL_FIELD_MODIFIED_BY_LABEL"
desc="Assign delivery to user" />

How to retrieve and insert the user id of the selected user in my database table using the MVC architecture, including all data bindings?

  • You may want to give a bit more information about the flow of your component or what you are trying to achieve. Are you submitting a form that you want to have your user's ID submitted with etc etc.. Welcome to the Joomla Stack Exchange. – Terry Carter Aug 7 '17 at 16:09
  • This is a backend part for a dispatch delivery system where we assign our users (who are drivers) delivery orders. On the component's page, when we press the new button, we can enter a new delivery and assign it to a registered user. We can also edit despatch orders. The code above goes into the models/forms dir. – Nederealm Aug 7 '17 at 16:42
  • Are you just trying to populate a dropdown with a list of your users names and their user id? If so I can give you the XML code in an answer to generate that form field. – Terry Carter Aug 7 '17 at 17:25
  • Check my answer below. It should have the desired effect you are looking for. – Terry Carter Aug 7 '17 at 17:32
1

Here is the code you would put in your models/forms XML file:

    <field
        name="modified_user_id"
        type="sql"
        label="JGLOBAL_FIELD_MODIFIED_BY_LABEL"
        description="Assign delivery to user"
        required="true"
        key_field="id"
        value_field="name"
        query="SELECT id, name FROM #__users ORDER BY name ASC"
    >
        <option value="">Choose a User</option>
    </field>

The SQL form field type is a type of list that generates a dropdown using the key and value fields specified in the field declaration.

You can get more information on the SQL field type here

  • Then in the $config['filter_fields'], is 'modified_user_id.id' binded with 'mytable.user_id'? – Nederealm Aug 8 '17 at 8:14
  • Using the SQL form field I posted above will allow you to save a specific userid to your table yes. You need to have your controller save method logic written in such a way that the INSERT or UPDATE function inserts that field where appropriate. $config['filter_fields'] is the setup for being able to sort by a specific field or set of fields in an array. So if you wanted to sort your table list by users then yes, use the name of your database table field. – Terry Carter Aug 8 '17 at 16:17
  • The easiest method of handling things is to have your field names match your database column names. Change modified_user_id to user_id for the quickest setup without needing to write a custom save method. – Terry Carter Aug 8 '17 at 16:19
  • It works. But how do I filter out administrator categories or super administrators? I tried filteringby user group id, but I still see my superadmin and users in the drop down – Nederealm Aug 11 '17 at 3:37

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.