sql >> Database >  >> RDS >> Mysql

codeigniter active records join met behulp van?

Er is geen ingebouwde ondersteuning voor JOIN ... USING in de actieve recordklasse. Je beste gok zou waarschijnlijk de join() . veranderen functie om zo te zijn (het bestand is system/database/DB_active_rec.php voor het geval je het niet weet)

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

Dus dan kun je dit gewoon in je code gebruiken join('table', 'USING ("something")')

Hoewel je misschien de klasse wilt uitbreiden in plaats van deze aan te passen, zodat je niet steeds hetzelfde hoeft te doen wanneer je je CI upgradet. Bekijk deze artikel of deze (of zoek op google) als u dit in plaats daarvan wilt doen.

Of als je al die problemen niet wilt doen, kun je een eenvoudige hulpfunctie schrijven die hetzelfde kan doen.

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

Laad later gewoon de helper en roep de functie als volgt aan join_using('table', 'key') . Het zal dan hetzelfde resultaat opleveren als met de originele join() behalve deze geeft je USING in plaats van ON staat.

Bijvoorbeeld:

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



  1. Entity Framework - MySQL - Probleem met datum/tijd-indeling

  2. Hoe rij bovenaan in mysql-query te plaatsen.

  3. Stapsgewijs upgradeproces voor R12.2 Upgrade Part -4 (Toepassing van 12.2.x Release Update Pack)

  4. SQL Server Failover Cluster Installatie -1