CodeIgniter Forums
Double select in builder - 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: Double select in builder (/showthread.php?tid=75694)



Double select in builder - RuudTableaux - 03-06-2020

For a database query im joining 2 tables. I want to add a second select since both tables have an id column. What i want:

Codeigniter 2:
PHP Code:
$this->db->select("*");
$this->db->select("main_table.id AS id");
$this->db->join("second_table","main_table.id = second_table.base_id","INNER");
$query $this->db->get("main_table"); 

Codeigniter 4:
PHP Code:
$db = \Config\Database::connect();
$builder $db->table('main_table');
$builder->select("*");
$builder->select("main_table.id AS id");
$builder->join("second_table","main_table.id = second_table.base_id","INNER");
$query $builder->get(); 


But in Codeigniter 4 this doesnt seem to work $query->getResult() returns just the id column.

Any ideas?


RE: Double select in builder - donpwinston - 03-06-2020

try $builder->select("second_table.*, main_table.id AS id");


RE: Double select in builder - RuudTableaux - 03-13-2020

thanks, ive managed to fix it as follows:
```
$db = \Config\Database::connect();
$builder = $db->table('main_table');

$builder->selects[] = "*";
$builder->selects[] = "main_table.id as id";

$builder->select($builder->selects);
$builder->join("second_table","main_table.id = second_table.base_id","INNER");
$query = $builder->get();
```

Seems the select needed to have an array.