A
A
Absolute1382020-12-18 13:08:23
PHP
Absolute138, 2020-12-18 13:08:23

How to correctly select from two tables by id array?

Now I have it and it works. But something tells me that I wrote g * vnokod)) Tell me
, please, how to do it right and wisely here?
GetVMUsersData is passed an array of user ids .

public function getVMUsersData($arr = array()){
    
    $db = JFactory::getDBO();
    
        $id_s = implode(',', $arr);
        $query = $db->getQuery(true);
        $query->select($db->quoteName(array('virtuemart_user_id', 'customer_number')));
        $query->from($db->quoteName('#__virtuemart_vmusers'));
        $query->where($db->quoteName('virtuemart_user_id') . ' IN(' . $id_s . ')');
        $db->setQuery($query);
        $results = $db->loadObjectList();
        
      unset($query);
      
      $query = $db->getQuery(true);
      $query->select($db->quoteName(array('virtuemart_user_id', 'last_name','first_name','city','school','class','teacher')));
      $query->from($db->quoteName('#__virtuemart_userinfos'));
      $query->where($db->quoteName('virtuemart_user_id') . ' IN(' . $id_s . ')');
      $db->setQuery($query);
      $temp = $db->loadObjectList();

      foreach($results as $k=>&$v){
        foreach($temp as $key=>$val){
          if($v->virtuemart_user_id == $val->virtuemart_user_id){
            $v->last_name = $val->last_name;
            $v->first_name = $val->first_name;
            $v->city = $val->city;
            $v->school = $val->school;
            $v->{'class'} = get_object_vars($val)['class'];
            $v->teacher = $val->teacher;
      
          }
    
        }
      }
        
      return $results;
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Rekun, 2020-12-21
@Absolute138

$id_s = implode(',', $arr);
$query = $db->getQuery(true);
$query->select(
  $db->quoteName(
    array('vv.virtuemart_user_id', 'vv.customer_number',  'vu.last_name', 'vu.first_name', 'vu.city', 'vu.school', 'vu.class', 'vu.teacher')
  )
);
$query->from($db->quoteName('#__virtuemart_vmusers', 'vv'));
$query->leftJoin($db->quoteName('#__virtuemart_userinfos', 'vu')) ' ON ' . $db->quoteName('vv.virtuemart_user_id') . ' = ' . $db->quoteName('vu.virtuemart_user_id'));
$query->where($db->quoteName('vv.virtuemart_user_id') . ' IN(' . $id_s . ')');
$db->setQuery($query);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question