Closed koyhoge closed 8 years ago
What is the output of:
$result = \Model_T1::query()
->related('t2')
->limit(1)->get();
$result = reset($result);
var_dump($result->to_array());
Is it identical or different? And what about
$result = \Model_T1::query()
->related('t2')
->rows_limit(1)->get();
$result = reset($result);
var_dump($result->to_array());
OK, I'll check generated SQL:
->limit(1)->get();
Outputs:
SELECT
`t0`.`id` AS `t0_c0`,
`t0`.`v1` AS `t0_c1`,
`t1_through`.`t2_id`,
`t1_through`.`t1_id`,
`t1`.`id` AS `t1_c0`,
`t1`.`v2` AS `t1_c1`
FROM (
SELECT `t0`.`id`, `t0`.`v1` FROM `t1` AS `t0` LIMIT 1
) AS `t0`
LEFT JOIN `t1_t2_map` AS `t1_through`
ON (`t0`.`id` = `t1_through`.`t1_id`)
LEFT JOIN `t2` AS `t1`
ON (`t1_through`.`t2_id` = `t1`.`id`)
and
->rows_limit(1)->get();
Outputs:
SELECT
`t0`.`id` AS `t0_c0`,
`t0`.`v1` AS `t0_c1`,
`t1_through`.`t2_id`,
`t1_through`.`t1_id`,
`t1`.`id` AS `t1_c0`,
`t1`.`v2` AS `t1_c1`
FROM `t1` AS `t0`
LEFT JOIN `t1_t2_map` AS `t1_through`
ON (`t0`.`id` = `t1_through`.`t1_id`)
LEFT JOIN `t2` AS `t1`
ON (`t1_through`.`t2_id` = `t1`.`id`)
LIMIT 1
So what are you saying? It returns only 1 related recoord with row_limit(1), but it depends internal design of the ORM. It's not about this issue.
It always returns a single related item with many_many relation on current code. Is this a correct behavoir?
rows_limit()
should return a single row from the SQL, causing relations to be incomplete, limit()
should always limit on the parent, and always return complete related record sets. So I just wanted to double-check.
@stevewest agree to merge this PR?
Looks ok to me.
I found bug with many_many relation. This P-R fix it.
Test environment
Create table
Inserted data
Model Classes
Reproduce bug
Result:
After this patch, correct result: