codeigniter的活动记录一起使用? [英] codeigniter active records join with using?

查看:120
本文介绍了codeigniter的活动记录一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写这个查询:

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

使用code点火器的活动记录类的,但是我没有ideda怎么办加入...使用

推荐答案

有没有内置的支持加入......在活动记录类使用。您最好的选择可能会改变加入()函数是这样的(该文件是系统/数据库/ DB_active_rec.php 如果你不知道)

There is no built-in support for JOIN ... USING in the active record class. Your best bet would probably change the join() function to be like this (the file is system/database/DB_active_rec.php in case you don't know)

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;
}

那么,你可以简单地使用你的code 加入('表','使用(东西)')

虽然,你可能要延长修改它的类,而不是让你不会需要做同样的事情,一遍又一遍,当你升级你的CI。 看一看这文章这个(或搜索谷歌),如果你想这样做的吧。

Although, you might want to extend the class instead of modifying it so that you won't need to do same thing over and over again when you upgrade your CI. Have a look at this article or this one (or search google) if you want to do so instead.

或者,如果你不想去所有的烦恼,你可以写一个简单的辅助功能,可以做同样的事情。

Or if you don't want to go all those troubles, you can write a simple helper function that can do the same thing.

<?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('表','键')。然后,它会产生相同的结果,你会与原来的加入()除了这一个会给你使用代替ON的 的条件。

Later on, just load the helper and call the function like this join_using('table', 'key'). It will then produce the same result as you would with the original join() except this one will give you USING instead of ON condition.

例如:

// $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);

这篇关于codeigniter的活动记录一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆