mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Fix index usage for OauthSessions relation from Account model
This commit is contained in:
41
common/models/OauthSessionQuery.php
Normal file
41
common/models/OauthSessionQuery.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use yii\db\ActiveQuery;
|
||||
|
||||
/**
|
||||
* @see \common\models\OauthSession
|
||||
*/
|
||||
class OauthSessionQuery extends ActiveQuery {
|
||||
|
||||
/**
|
||||
* The owner_id field in the oauth_sessions table has a string type.
|
||||
* If you try to search using an integer value, the MariaDB will not apply the index, which will cause
|
||||
* a huge rows scan.
|
||||
*
|
||||
* After examining the query builder logic in Yii2, we managed to find a solution to bring the value
|
||||
* that the builder will use to create a link to the string exactly before the construction
|
||||
* and restore the original value afterwards.
|
||||
*
|
||||
* @param $builder
|
||||
* @return ActiveQuery|\yii\db\Query
|
||||
*/
|
||||
public function prepare($builder) {
|
||||
$idHasBeenCastedToString = false;
|
||||
if ($this->primaryModel instanceof Account && $this->link === ['owner_id' => 'id']) {
|
||||
$this->primaryModel->id = (string)$this->primaryModel->id;
|
||||
$idHasBeenCastedToString = true;
|
||||
}
|
||||
|
||||
$query = parent::prepare($builder);
|
||||
|
||||
if ($idHasBeenCastedToString) {
|
||||
$this->primaryModel->id = (int)$this->primaryModel->id;
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user