New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding BaseQuery that provides concrete implementation for Query #19761
base: master
Are you sure you want to change the base?
Conversation
Thank you for putting effort in the improvement of the Yii framework. Unfortunately a use case is missing. It is required to get a better understanding of the pull request and helps us to determine the necessity and applicability of the suggested change to the framework. Could you supply us with a use case please? Please be as detailed as possible and show some code! Thanks! This is an automated comment, triggered by adding the label |
Our company uses mysql as a database. We need to use indexes to optimize database queries. We want to add globally additional methods to set these indexes in order to use them in all of our Query classes (ActiveQuery and not only). We were surprised (and still are) that there is no way to extend the Query class without crutches. Indexes are one example. We have quite a few improvements that require extending the functionality of the Query class |
What about DI? For example you can define your query class namespace common\db;
class Query extends \yii\db\Query
{
} and define in config
|
Простите, я так понимаю, что тут на русском могут. Проще объяснить... Данное решение - это первое что мы попробовали, вроде очевидное действие, но нам оно не помогло. С доработкой QueryBuilder нет проблем. Добавил расширенный yii\db\Connection и указал свой класс CustomQueryBuilder, например: class CustomDbConnection extends Connection {
/**
* @return CustomQueryBuilder
*/
public function getQueryBuilder() {
return new CustomQueryBuilder($this->getSchema()->db);
}
} Указал в definitions Изучив код yii2 досконально, приходишь к выводу, что |
Why not extend |
Because we use these methods not only in the context of ActiveQuery. I don't understand how it happened that the Query class can't be overridden in a simple way |
That does not explain what is the problem with solution I proposed. If you want to use your custom methods, then use
This BaseSomething convention used to overwrite some Yii classes is more like a hack and it is limited to helpers with static methods. For things that are actually instantiated you should use DI-like solutions. |
There is no way to extend the
yii/db/Query
class.There is a
Yii::$classMap
, but then you will have to copy all the code to a new class and maintain it manually or use patches.This solution solves the problem.