php - How to select username in a given query in Joomla?

321

Helllo,

I have my own Joomla table and I select everything from it to json_encode the result. How can I manipulate some fields in $db->loadObjectList()? I need username from #__users for a given userId.

Here's my code:

<?php
    $query = $db->getQuery(true)
             ->select('*') // fields a and b from this table are userIds
             ->from('#__my_custom_table');
    $db->setQuery($query);
    $result = $db->loadObjectList();

/* how can I select the username from #__users where #__my_custom_table.a and .b is like #__users.id to add these information to $result
I want to override these fields from #__my_custom_table: a, b
*/

  foreach ($result as $row) {
            $row->a = JFactory::getUser($row->a)->name; 
// how to override this in $result?
  }

   echo json_encode($result, JSON_FORCE_OBJECT);
87

Answer

Solution:

Something like this:

$query = $db->getQuery(true)
             ->select('t.*, u.username')
             ->from('#__my_custom_table t')
             ->join('LEFT', '#__users u ON t.a = u.id');

People are also looking for solutions to the problem: javascript - Ajax works only on first id submitted (with php from db)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.