Gdn_SQLDriver::join PHP Method

join() public method

The table(s) to which this query should join. Returns this object for chaining purposes.
public join ( string $TableName, string $On, string $Join = '' ) : Gdn_SQLDriver
$TableName string The name of a single table to join to.
$On string The conditions on which the join should occur. ie. "user.role_id = role.id"
$Join string The type of join to be made. Accepted values are: 'inner', 'outer', 'left', 'right', 'left outer', and 'right outer'.
return Gdn_SQLDriver $this
    public function join($TableName, $On, $Join = '')
    {
        $Join = strtolower(trim($Join));
        if ($Join != '' && !in_array($Join, array('inner', 'outer', 'left', 'right', 'left outer', 'right outer'), true)) {
            $Join = '';
        }
        // Add the table prefix to any table specifications in the clause
        // echo '<div>'.$TableName.' ---> '.$this->EscapeSql($this->Database->DatabasePrefix.$TableName, TRUE).'</div>';
        if ($this->Database->DatabasePrefix) {
            $TableName = $this->mapAliases($TableName);
            //$Aliases = array_keys($this->_AliasMap);
            //$Regex = '';
            //foreach ($Aliases as $Alias) {
            //   $Regex .= '(?<! '.$Alias.')';
            //}
            //$Regex = '/(\w+'.$Regex.'\.)/';
            //$On = preg_replace($Regex, $this->Database->DatabasePrefix.'$1', ' '.$On);
        }
        $JoinClause = ltrim($Join . ' join ') . $this->escapeIdentifier($TableName, true) . ' on ' . $On;
        $this->_Joins[] = $JoinClause;
        return $this;
    }

Usage Example

 /**
  *
  *
  * @param Gdn_SQLDriver $Sql
  * @param $Tag
  * @param $Limit
  * @param int $Offset
  * @param string $Op
  * @throws Exception
  */
 protected function _setTagSql($Sql, $Tag, &$Limit, &$Offset = 0, $Op = 'or')
 {
     $SortField = 'd.DateLastComment';
     $SortDirection = 'desc';
     $TagSql = clone Gdn::sql();
     if ($DateFrom = Gdn::request()->get('DateFrom')) {
         // Find the discussion ID of the first discussion created on or after the date from.
         $DiscussionIDFrom = $TagSql->getWhere('Discussion', array('DateInserted >= ' => $DateFrom), 'DiscussionID', 'asc', 1)->value('DiscussionID');
         $SortField = 'd.DiscussionID';
     }
     $Tags = array_map('trim', explode(',', $Tag));
     $TagIDs = $TagSql->select('TagID')->from('Tag')->whereIn('Name', $Tags)->get()->resultArray();
     $TagIDs = array_column($TagIDs, 'TagID');
     if ($Op == 'and' && count($Tags) > 1) {
         $DiscussionIDs = $TagSql->select('DiscussionID')->select('TagID', 'count', 'CountTags')->from('TagDiscussion')->whereIn('TagID', $TagIDs)->groupBy('DiscussionID')->having('CountTags >=', count($Tags))->limit($Limit, $Offset)->orderBy('DiscussionID', 'desc')->get()->resultArray();
         $Limit = '';
         $Offset = 0;
         $DiscussionIDs = array_column($DiscussionIDs, 'DiscussionID');
         $Sql->whereIn('d.DiscussionID', $DiscussionIDs);
         $SortField = 'd.DiscussionID';
     } else {
         $Sql->join('TagDiscussion td', 'd.DiscussionID = td.DiscussionID')->limit($Limit, $Offset)->whereIn('td.TagID', $TagIDs);
         if ($Op == 'and') {
             $SortField = 'd.DiscussionID';
         }
     }
     // Set up the sort field and direction.
     saveToConfig(array('Vanilla.Discussions.SortField' => $SortField, 'Vanilla.Discussions.SortDirection' => $SortDirection), '', false);
 }