yii2
yii2 copied to clipboard
Why not used DI-container in \yii\db\Schema ?
Because of this, you must override the schema classes for each driver. It was much more comfortable with DI container.
Especially for these methods:
/**
* Creates a query builder for the database.
* This method may be overridden by child classes to create a DBMS-specific query builder.
* @return QueryBuilder query builder instance
*/
public function createQueryBuilder()
{
return new QueryBuilder($this->db);
}
/**
* Create a column schema builder instance giving the type and value precision.
*
* This method may be overridden by child classes to create a DBMS-specific column schema builder.
*
* @param string $type type of the column. See [[ColumnSchemaBuilder::$type]].
* @param integer|string|array $length length or precision of the column. See [[ColumnSchemaBuilder::$length]].
* @return ColumnSchemaBuilder column schema builder instance
* @since 2.0.6
*/
public function createColumnSchemaBuilder($type, $length = null)
{
return new ColumnSchemaBuilder($type, $length);
}
how would it be easier with DI?
For example, we need custom ColumnSchemaBuilder class (with extended implementation). For this currently we should extend \yii\db\mysql\Schema class (only for overriding of method \yii\db\Schema::createColumnSchemaBuilder) for each driver.
But in the case:
public function createColumnSchemaBuilder($type, $length = null)
{
return \Yii::createObject(\yii\db\ColumnSchemaBuilder::className(), [$type, $length]);
}
we can resolve dependency (in entry script) like this (without overriding of \yii\db\mysql\Schema):
\Yii::$container->set(\yii\db\ColumnSchemaBuilder::className(), ['class' => \app\component\ColumnSchemaBuilder::className()]);
It is more convenient and easy.
@cebe For Schema
this is 2 lines of code changes.
Could you add label ready for adoption
or close this?
Has this been implemented yet?
It looks like it has not, as when I set
\Yii::$container->set(\yii\db\sqlite\QueryBuilder::class, ['class' => \santilin\sqlite\QueryBuilder::class]);
the instantiated class is the original yii\db\sqlite\QueryBuilder
.
I was hoping that I could set a different query builder for some connections like this:
'class' => 'yii\db\Connection',
'dsn' => 'sqlite:@app/runtime/my.db',
'queryBuilder' => santilin\sqlite\QueryBuilder::class,
but it doesn't work for the same reason stated above.
I am using the development version of Yii2.
@santilin no, it was not implemented.