[UPDATED]
the error is now gone but the data result from the codeigniter 4 query builder all has the same id
value, unlike the one in phpmyadmin. and if I use the $builder
method it now gives the expected result, but I'm still hoping to be able to fully use the query builder properly if possible..
I have a mysql table like this
id | nama | hari
-----------------
1 | AAA | 1
2 | AAA | 2
3 | AAA | 3
4 | AAA | 4
5 | AAA | 5
6 | BBB | 1
7 | BBB | 2
8 | BBB | 3
when I get a parameter id
1, I want to get the row number 1-5 only. it works when I use this sql in phpmyadmin
select m.*
from m_rute m
join (
select m1.*
from m_rute m1
where m1.id = 1
) mt on mt.nama_rute = m.nama_rute
then I try to "translate" it using codeigniter 4's query builder but when I test it on postman, it gives a set of data where all the id
field is the same, unlike the one in phpmyadmin result.
controller
public function show($id = null)
{
$db = \Config\Database::connect();
$builder = $db->table('m_rute as m');
$builder->select('m.*')->where('m.id', $id);
$subquery = $builder->getCompiledSelect();
// this works but wouldn't a fully proper query builder seem nicer?
//////////////
// $builder->select('m.*')
// ->join('('.$subquery.') as m1', 'm1.nama_rute = m.nama_rute')
// ;
// $q = $builder->get();
// $data = $q->getResultArray();
$model = new MRuteModel();
$model->join('('.$subquery.') as t', 't.nama_rute = m_rute.nama_rute');
$data = $model->findAll();
if (!$data) {
return $this->failNotFound('Data not found');
}
return $this->respond($data);
}
any help is appreciated
Solution 1: Using Raw/Regular Queries.
Solution 2A: Using SQL Joins With Raw Queries.
Solution 2B: Using SQL Joins With A Query Builder.
getCompiledSelect()
Solution 3A: Using A
where
Clause With A Closure.Solution 3B: Using A
where
Clause With A Builder.