Restored previous entity retrieval
Using temporary tables turned out to be more expensive on bigger databases. Restoring two queries version.
This commit is contained in:
parent
e93c3588f9
commit
992b9ba5ac
1 changed files with 5 additions and 32 deletions
|
@ -72,38 +72,11 @@ abstract class AbstractModel extends RedBean_SimpleModel
|
||||||
|
|
||||||
public static function getEntitiesWithCount($query, $perPage = null, $page = 1)
|
public static function getEntitiesWithCount($query, $perPage = null, $page = 1)
|
||||||
{
|
{
|
||||||
$table = static::getTableName();
|
return
|
||||||
$tempTable = $table . '_temp';
|
[
|
||||||
|
self::getEntities($query, $perPage, $page, true),
|
||||||
R::begin();
|
self::getEntityCount($query)
|
||||||
R::exec('CREATE TEMPORARY TABLE ' . $tempTable . '(id INTEGER)');
|
];
|
||||||
R::exec('CREATE INDEX idx_fk_' . $tempTable . '_id ON ' . $tempTable . '(id)');
|
|
||||||
|
|
||||||
$dbQuery = R::$f->getNew()->begin();
|
|
||||||
$dbQuery->insertInto($tempTable . '(id)');
|
|
||||||
$dbQuery->select($table . '.id');
|
|
||||||
$builder = static::getQueryBuilder();
|
|
||||||
if ($builder)
|
|
||||||
$builder::build($dbQuery, $query);
|
|
||||||
else
|
|
||||||
$dbQuery->from($table);
|
|
||||||
$dbQuery->get();
|
|
||||||
|
|
||||||
$count = intval(R::getCell('SELECT COUNT(*) FROM ' . $tempTable));
|
|
||||||
$rows = R::$f->getNew()
|
|
||||||
->begin()
|
|
||||||
->select($table . '.*')
|
|
||||||
->from($tempTable)
|
|
||||||
->innerJoin($table)
|
|
||||||
->on($table . '.id = ' . $tempTable . '.id')
|
|
||||||
->limit('?')->put($perPage)
|
|
||||||
->offset('?')->put(($page - 1) * $perPage)
|
|
||||||
->get();
|
|
||||||
|
|
||||||
R::rollback();
|
|
||||||
|
|
||||||
$entities = self::convertRows($rows, $table, true);
|
|
||||||
return [$entities, $count];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function create()
|
public static function create()
|
||||||
|
|
Loading…
Reference in a new issue