mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-12 14:58:42 +00:00
Improve many-to-many query performance
This commit is contained in:
parent
0fd9b3edd7
commit
acf9a247f0
|
|
@ -26,8 +26,9 @@ use phpOMS\Utils\ArrayUtils;
|
||||||
* @link https://jingga.app
|
* @link https://jingga.app
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*
|
*
|
||||||
* @todo Add memory cache per read mapper parent call (These should be cached: attribute types, file types, etc.)
|
* @todo Add memory cache per read mapper parent call (These should be cached: attribute types, file types, etc.)
|
||||||
* @todo Add getArray functions to get array instead of object
|
* @todo Add getArray functions to get array instead of object
|
||||||
|
* @todo Allow to define columns in all functions instead of members?
|
||||||
*
|
*
|
||||||
* @template R
|
* @template R
|
||||||
*/
|
*/
|
||||||
|
|
@ -862,37 +863,17 @@ final class ReadMapper extends DataMapperAbstract
|
||||||
}
|
}
|
||||||
|
|
||||||
$objectMapper = $this->createRelationMapper($many['mapper']::get(db: $this->db), $member);
|
$objectMapper = $this->createRelationMapper($many['mapper']::get(db: $this->db), $member);
|
||||||
if ($many['external'] === null) {
|
if ($many['external'] === null/* same as $many['table'] !== $many['mapper']::TABLE */) {
|
||||||
$objectMapper->where($many['mapper']::COLUMNS[$many['self']]['internal'], $primaryKey);
|
$objectMapper->where($many['mapper']::COLUMNS[$many['self']]['internal'], $primaryKey);
|
||||||
} else {
|
} else {
|
||||||
// @todo: don't do this, even in a many-many relationship I should be able to use joins which the above if branch effectively is!
|
|
||||||
|
|
||||||
$query = new Builder($this->db, true);
|
$query = new Builder($this->db, true);
|
||||||
$src = $many['external'] ?? $many['mapper']::PRIMARYFIELD;
|
$query->leftJoin($many['table'])
|
||||||
|
->on($many['mapper']::TABLE . '_d1.' . $many['mapper']::PRIMARYFIELD, '=', $many['table'] . '.' . $many['external'])
|
||||||
// @todo: what if a specific column name is defined instead of primaryField for the join? Fix, it should be stored in 'column'
|
|
||||||
$query->select($many['table'] . '.' . $src)
|
|
||||||
->from($many['table'])
|
|
||||||
->where($many['table'] . '.' . $many['self'], '=', $primaryKey);
|
->where($many['table'] . '.' . $many['self'], '=', $primaryKey);
|
||||||
|
|
||||||
if ($many['table'] !== $many['mapper']::TABLE) {
|
// Cannot use join, because join only works on members and we don't have members for a relation table
|
||||||
$query->leftJoin($many['mapper']::TABLE)
|
// This is why we need to create a "base" query which contians the join on table columns
|
||||||
->on($many['table'] . '.' . $src, '=', $many['mapper']::TABLE . '.' . $many['mapper']::PRIMARYFIELD);
|
$objectMapper->query($query);
|
||||||
}
|
|
||||||
|
|
||||||
$sth = $this->db->con->prepare($query->toSql());
|
|
||||||
if ($sth === false) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$sth->execute();
|
|
||||||
$result = $sth->fetchAll(\PDO::FETCH_COLUMN);
|
|
||||||
|
|
||||||
if (empty($result)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$objectMapper->where($many['mapper']::COLUMNS[$many['mapper']::PRIMARYFIELD]['internal'], $result, 'IN')->execute();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$objects = $objectMapper->execute();
|
$objects = $objectMapper->execute();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user