Yii2 ActiveQuery join keep returns distinct values

Viewed 923

I have two tables as below

table halte :

CREATE TABLE `halte` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nama` varchar(255) NOT NULL,
  `lat` float(10,6) DEFAULT NULL,
  `lng` float(10,6) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

table stops :

CREATE TABLE `stops` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_halte` int(11) DEFAULT NULL,
  `sequence` int(2) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `id_halte` (`id_halte`)
)

I also have some other tables which don't cause any problems.

Halte table has many to one relation to stop. The problem is when i try to get rows from halte table using right join to table stops, Yii only returns unique rows. Yii won't return same halte's row more once even stop table has more than one record related to same row in halte table.

Here's my code

$haltes = $modelHalte->find()
                    ->rightJoin('stops', 'halte.id = stops.id_halte')
                    ->where(['stops.id_rute'=>Yii::$app->request->get('rute')])
                    ->orderBy('sequence')
                    ->all();

I have tried distinct(false) but no result. I've also check debugger and it run right query i want :

SELECT `halte`.* FROM `halte` RIGHT JOIN `stops` ON halte.id = stops.id_halte WHERE `stops`.`id_rute`='1' ORDER BY `sequence`

I tried to run that query manually and it returned 29 rows which is what what i want. But in Yii, it only returned 27 rows because 2 rows is same record in halte table.

I know i can achieve this using yii\db\Query, but i want to use ActiveRecord.

Are there any way to work around this?
I would really appreciate your opinion/help.
Thanks.

1 Answers
Related