1

So I have a query in the getlistquery method of my model, except that it relies on data in another table, that sometimes is not filled. This gives me an error when that other data is not available.

But it seems that the getlistquery must return a query, I tried to use the return false but that does not work. Do I need to move that code to another method? Like getItems?

protected function getListQuery()
{
    
    $user = Factory::getUser();
    $db = $this->getDbo();
    $query = $db
        ->getQuery(true)
        ->select('*')
        ->from($db->quoteName('#__table_profile'))
        ->where($db->quoteName('user_id') . " = " . $db->quote($user->id));

    $db->setQuery($query);
    $result = $db->loadAssoc();

    //no user profile available, return false
    if(!$result) return false;
}

But now I get the following error: u have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 20' at line 1

Probably because I don't return a query and now it is not complete.

6

2 Answers 2

2

The function getListQuery returns \Joomla\Database\DatabaseQuery and nothing else.

The returned query is driven by populateState() which in turn tells getListQuery what to do. Well, populateState just fills data before getListQuery gets executed. You can do everything inside getListQuery().

The main state of your component is known, becase you do some things inside the controller. Then you pass data to populateState() so it tells getListQuery how to behave.

This way you can build your query conditionally and it will simply return empty result for that query, because conditions have not been met. It's all about configuring the state of the model, then getListQuery returns a given data, so no more functions are needed.

It's all about telling ListModel how to return data via getListQuery. This way you don't implement many public functions to return objects. In Joomla it's done via one mega-function, in laravel there are scopes and many local functions that build up an overall query. In both cases it gives same results but differ in controller implementation.

1

I decided to skip the getListQuery and moved the code into a custom function and decided to use that to get my items. I couldn't get it to work with the getListQuery, and although it's cleaner code to use the standard methods of Joomla and overriding them, this worked for me a lot better, without headaches.

public function getProfiles()
{
    
    $user = Factory::getUser();
    $db = $this->getDbo();
    $query = $db
        ->getQuery(true)
        ->select('*')
        ->from($db->quoteName('#__table_profile'))
        ->where($db->quoteName('user_id') . " = " . $db->quote($user->id));

    $db->setQuery($query);
    $result = $db->loadAssoc();

    //no user profile available, return false
    if(!$result) return false;
}

In my view.html.php I use this line of code to get all the profiles:

$this->profiles = $this->get('Profiles');

And in my template I check if the profiles exists, and show code based on that value.

1
  • So you did almost the same the getlistquery does. The standard !J getListQuery() goes like: $db = ...; $query = $db->getQuery(true); $query->select('*')->from(...)->where(...); AND then return $query; In controller you call $results = $model->getListQuery()->loadAssoc(); Well, you simply made yourself unable to use your model anywhere, because you've already returned database data as assoc, so no one is able to retrieve your model and configure its return value. I encourage you to learn things from articles/article models to see how models in 'kind of' MVC work.
    – Elboyler
    Commented Nov 19, 2022 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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