Hi-
When I used:
I get a result.
However, that is not the correct way to do things, so I updated my code to:
But I do not get a result and there is no error. What am I doing wrong in the second example? All I can think of is that the joins are not behaving as expected, so the result ends up NULL, but I have no idea how I would rewrite it so it works correctly. Any help is much appreciated.
When I used:
Code:
use Joomla\CMS\Factory;$user = Factory::getUser();$db = Factory::getContainer()->get('DatabaseDriver');$userId = $user->get('id');$db->setQuery('SELECT u.name, u.email, u.username, p.*, s.sig_form, s.year FROM (`#__users` u JOIN `#__jsn_users` p ON u.id = p.id) LEFT JOIN `#__jsn_sig_form` s ON u.id = s.user_id AND s.year = YEAR(CURDATE()) WHERE u.id = '.$userId);$results = $db->loadAssoc();
However, that is not the correct way to do things, so I updated my code to:
Code:
use Joomla\CMS\Factory;$user = Factory::getUser();$db = Factory::getContainer()->get('DatabaseDriver');$userId = $user->get('id');$year = date('Y');$query = $db->getQuery(true); $query ->select('u.name', 'u.email', 'u.username', 'p.*', 's.sig_form', 's.year') ->from($db->quoteName('#__users', 'u')) ->join('INNER', $db->quoteName('#__jsn_users', 'p').' ON ('.$db->quoteName('u.id').' = '.$db->quoteName('p.id').')') ->join('LEFT', $db->quoteName('#__jsn_sig_form', 's').' ON ('.$db->quoteName('u.id').' = '.$db->quoteName('s.user_id').' AND '.$db->quoteName('s.year').' = :year)') ->where($db->quoteName('u.id').' = :userid') ->bind(':userid', $userId) ->bind(':year', $year);$db->setQuery($query);$results = $db->loadAssoc();
Statistics: Posted by kgriffin — Tue Aug 13, 2024 5:00 pm