Linked mssql db - getTableSchema problem

Hi everyone,

I’ve met a problem about linked db with MSSQL. I have two servers which are hosting different db. On my local server, I set the remote one thanks to linked db functionality so that I can request db of both servers.

Below a model from remote db.


class FCONTACTT extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'AIG-FIC.'.yii::$app->params['parametres']['dbSAGE'].'.dbo.F_CONTACTT';

    }


    /**

     * @return \yii\db\Connection the database connection used by this AR class.

     */

    public static function getDb()

    {

        return Yii::$app->get('dbSAGE');

    }


}

Below error I get.

Below a statement which works and returns expected results.

But the next requests are bad due to the fake name of table:

I guess the problem is coming from getTableSchema() of the model ActiveRecord.


  

    /**

     * Declares the name of the database table associated with this AR class.

     * By default this method returns the class name as the table name by calling [[Inflector::camel2id()]]

     * with prefix [[Connection::tablePrefix]]. For example if [[Connection::tablePrefix]] is 'tbl_',

     * 'Customer' becomes 'tbl_customer', and 'OrderItem' becomes 'tbl_order_item'. You may override this method

     * if the table is not named after this convention.

     * @return string the table name

     */

    public static function tableName()

    {

        return '{{%' . Inflector::camel2id(StringHelper::basename(get_called_class()), '_') . '}}';

    }

  

     /**

     * Returns the schema information of the DB table associated with this AR class.

     * @return TableSchema the schema information of the DB table associated with this AR class.

     * @throws InvalidConfigException if the table for the AR class does not exist.

     */

    public static function getTableSchema()

    {

        $tableSchema = static::getDb()

            ->getSchema()

            ->getTableSchema(static::tableName());


        if ($tableSchema === null) {

            throw new InvalidConfigException('The table does not exist: ' . static::tableName());

        }


        return $tableSchema;

    }

How could I fix this problem ?

Best regards

Edit:

(Currently trying to override the tableName() function)

Edit2: I’m wrong with getTableSchema

I’ve probably understood what is wrong in it. I don’t have table_schema from the linked server. I’m going to search.




/**

     * Resolves the table name and schema name (if any).

     * @param TableSchema $table the table metadata object

     * @param string $name the table name

     */

    protected function resolveTableNames($table, $name)

    {

        $parts = explode('.', str_replace(['[', ']'], '', $name));

        $partCount = count($parts);

        if ($partCount === 3) {

            // catalog name, schema name and table name passed

            $table->catalogName = $parts[0];

            $table->schemaName = $parts[1];

            $table->name = $parts[2];

            $table->fullName = $table->catalogName . '.' . $table->schemaName . '.' . $table->name;

        } elseif ($partCount === 2) {

            // only schema name and table name passed

            $table->schemaName = $parts[0];

            $table->name = $parts[1];

            $table->fullName = $table->schemaName !== $this->defaultSchema ? $table->schemaName . '.' . $table->name : $table->name;

        } else {

            // only table name passed

            $table->schemaName = $this->defaultSchema;

            $table->fullName = $table->name = $parts[0];

        }

    }

https://github.com/yiisoft/yii2/pull/12293

Current pull request to solve this issue.