I have two tables as below
我有两张桌子如下
table halte :
表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.
Halte表有多对一的关系可以停止。问题是当我尝试使用右连接到表停止从halte表中获取行时,Yii只返回唯一的行。一旦偶数停止表与halte表中的同一行相关的多条记录,Yii将不再返回相同的halte行。
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.
我试图手动运行该查询,它返回29行,这就是我想要的。但是在Yii中,它只返回了27行,因为2行是halte表中的相同记录。
I know i can achieve this using yii\db\Query
, but i want to use ActiveRecord.
我知道我可以使用yii \ db \ Query实现这一点,但我想使用ActiveRecord。
Are there any way to work around this?
I would really appreciate your opinion/help.
Thanks.
有什么方法可以解决这个问题吗?我非常感谢您的意见/帮助。谢谢。
1 个解决方案
#1
1
Check the sql command generated by you active query
检查您的活动查询生成的sql命令
$haltes = $modelHalte->find()
->rightJoin('stops', 'halte.id = stops.id_halte')
->where(['stops.id_rute'=>Yii::$app->request->get('rute')])
->orderBy('sequence')
->all();
echo $haltes->createCommand()->sql;
or to get the SQL with all parameters included try:
或者要获取包含所有参数的SQL,请尝试:
$haltes->createCommand()->getRawSql();
And compare the code generated by ActiveQuery with your created manually ..
并将ActiveQuery生成的代码与您手动创建的代码进行比较。
#1
1
Check the sql command generated by you active query
检查您的活动查询生成的sql命令
$haltes = $modelHalte->find()
->rightJoin('stops', 'halte.id = stops.id_halte')
->where(['stops.id_rute'=>Yii::$app->request->get('rute')])
->orderBy('sequence')
->all();
echo $haltes->createCommand()->sql;
or to get the SQL with all parameters included try:
或者要获取包含所有参数的SQL,请尝试:
$haltes->createCommand()->getRawSql();
And compare the code generated by ActiveQuery with your created manually ..
并将ActiveQuery生成的代码与您手动创建的代码进行比较。