CodeIgniter Forums
Can CI accept a variable as the full join syntax? [SOLVED] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Can CI accept a variable as the full join syntax? [SOLVED] (/showthread.php?tid=79811)



Can CI accept a variable as the full join syntax? [SOLVED] - eri146 - 07-29-2021

I created a model which supports a generic database GET request and if you include data from the $joinArray then it will loop through the values of this array and create $builder->join($joinArray);

Unfortunately I can't get this to work.

To keep it short I would like to do the following:
Code:
    $joinData = array(
        0 => "'table AS k', 'k.something = j.anotherthing', 'left'"
    );

    if(isset($joinData)){
        foreach($joinData as $key => $value){
            $builder->join($value);
        }
    }

In the end, it should behave like this:
     $builder->join('table AS k', 'k.something = j.anotherthing', 'left');

thank you for your time!

Answered, look below for the code if you're having the same issue as me


RE: Can CI accept a variable as the full join syntax? - davis.lasis - 07-30-2021

PHP Code:
$joinData = [
            0 => [
                'table' => 'table AS k',
                'condition' => 'k.something = j.anotherthing',
                'type' => 'left',
            ],
            1 => [
                'table' => 'table AS t',
                'condition' => 't.something = j.anotherthing',
                'type' => 'left',
            ],
        ];

        if (!empty($joinData)) {
            foreach($joinData as $key => $join){
                $builder->join($join['table'], $join['condition'], $join['type']);
            }
        



RE: Can CI accept a variable as the full join syntax? - eri146 - 07-30-2021

Thank you for your response!

The answer provided works like a charm. I was also pointing to a very similar but wrong function which didn't help matters. hah.