Where query in Joomla component - what file do I edit?

by Nathanael   Last Updated April 11, 2017 13:10 PM

Okay, so I am completely new to Joomla and I am trying to edit the source files of a component I built using component-creator.com.

I have a database which stores different data for multiple different users, and I want to design several views where the logged-in user can view only the data associated with their own user name in the database. Currently, every user can see a listing of the data of every other user in the database, which is unacceptable for security reasons. I assume that I need to somehow retrieve the current session user id and use a WHERE query to filter the list view down to only the database entries associated with that user id.

First off, what files do I need to edit? Component-creator seems to suggest here that I need to edit views/students/view.html.php, while some other forums seem to point to models/students.php. models/students.php seems to already have several SQL queries with chaining and WHERE clauses, but I wanted to make sure.

Secondly, how do I retrieve and/ or pass the current session user id? Do I somehow need to pass that variable from views/students/tmpl/default.php, or can I retrieve it directly from within models/students.php?

Any insight is appreciated. Thank you!

Answers 1

As you've probably gathered already, Joomla is an MVC (model, view controller) content management system, so you're database queries will be located in the model:


As for current user session, you can get the current user object like so:

$user = JFactory::getUser();

Or if it's the actual session ID you wish to get:

$session = JFactory::getSession();
echo $session->getId();

Both of which you can access in any part of the component, so yes, that includes the model.

Hope this helps

April 11, 2017 12:51 PM

Related Questions

Join two list views without a shared key

Updated August 28, 2017 16:10 PM

Need help writing a form to a table

Updated February 24, 2018 10:10 AM