yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

Why not used DI-container in \yii\db\Schema ?

Open IStranger opened this issue 9 years ago • 5 comments

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

IStranger avatar Sep 21 '15 09:09 IStranger

how would it be easier with DI?

cebe avatar Sep 21 '15 10:09 cebe

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.

IStranger avatar Sep 21 '15 15:09 IStranger

@cebe For Schema this is 2 lines of code changes. Could you add label ready for adoption or close this?

SamMousa avatar Mar 30 '17 12:03 SamMousa

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 avatar Jun 13 '19 18:06 santilin

@santilin no, it was not implemented.

samdark avatar Jun 13 '19 20:06 samdark