Fix index usage for OauthSessions relation from Account model

This commit is contained in:
ErickSkrauch 2019-08-08 02:47:36 +03:00
parent 25df1c711a
commit 89f7195a37
2 changed files with 45 additions and 0 deletions

View File

@ -28,6 +28,10 @@ class OauthSession extends ActiveRecord {
return '{{%oauth_sessions}}';
}
public static function find(): OauthSessionQuery {
return new OauthSessionQuery(static::class);
}
public function behaviors() {
return [
[

View 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;
}
}