【问题标题】:codeigniter active records join with using?使用codeigniter活动记录加入?
【发布时间】:2011-09-11 13:15:24
【问题描述】:

我想写这个查询:

SELECT u.userId, uil.interestId, url.regionId FROM users u 
  JOIN userInterestLink uil USING (userId)
  JOIN userRegionLink url USING (userId)
  JOIN dealInterestLink dil USING (interestId)
  JOIN dealRegionLink drl USING (regionId, dealId)
WHERE dealId = 1

使用代码点火器活动记录类,但是我不知道如何执行JOIN ... USING

【问题讨论】:

    标签: mysql codeigniter activerecord


    【解决方案1】:

    活动记录类中没有对JOIN ... USING 的内置支持。您最好的选择可能是将join() 函数更改为这样(文件为system/database/DB_active_rec.php,以防您不知道)

    public function join($table, $cond, $type = '')
    {
        if ($type != '')
        {
            $type = strtoupper(trim($type));
    
            if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
            {
                $type = '';
            }
            else
            {
                $type .= ' ';
            }
        }
    
        // Extract any aliases that might exist.  We use this information
        // in the _protect_identifiers to know whether to add a table prefix
        $this->_track_aliases($table);
    
        // Strip apart the condition and protect the identifiers
        if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
        {
            $match[1] = $this->_protect_identifiers($match[1]);
            $match[3] = $this->_protect_identifiers($match[3]);
    
            $cond = $match[1].$match[2].$match[3];
        }
    
        // Assemble the JOIN statement
        $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE);
    
        $using_match = preg_match('/using[ (]/i', $cond);
    
        if ($using_match)
        {
            $join .= $cond;
        }
        else
        {
            $join .= ' ON '.$cond;
        }
    
        $this->ar_join[] = $join;
        if ($this->ar_caching === TRUE)
        {
            $this->ar_cache_join[] = $join;
            $this->ar_cache_exists[] = 'join';
        }
    
        return $this;
    }
    

    那么,您可以简单地在代码中使用它join('table', 'USING ("something")')

    不过,您可能希望扩展类而不是修改它,这样在升级 CI 时就不需要一遍又一遍地做同样的事情。 如果您想这样做,请查看此 articlethis one(或搜索 google)。

    或者,如果您不想遇到所有这些麻烦,您可以编写一个简单的辅助函数来做同样的事情。

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    function join_using($table, $key) 
    {
        $CI = get_instance();
        $join = 'JOIN '. $table .' USING (`'. $key .'`)';
        return $CI->db->ar_join[] = $join;
    }  
    

    稍后,只需加载帮助程序并像这样调用函数join_using('table', 'key')。然后它会产生与原始join() 相同的结果,除了这个会为您提供USING 而不是ON 条件。

    例如:

    // $something1 and $something2 will produce the same result.
    $something1 = $this->db->join('join_table', 'join_table.id = table.id')->get('table')->result();
    print_r($something1);
    join_using('join_table', 'id');
    $something2 = $this->db->get('table')->result();
    print_r($something2);
    

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      相关资源
      最近更新 更多