-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
joinWith + criteria over multiple databases not working #5470
Comments
Of course you cannot. What you expected? $employeesShop1 = \app\models\Employee::find()
->joinWith(['shop' => function($query) {
return $query->from('yii2_test1.' . Shop::tableName())
->andWhere(['shop_id' => 1]);
}])
->all(); I am not sure it will work :D |
It is possible when using two MySQL instances as far as I know but the query should look as @mdmunir explained. |
It definitely isn't possible if databases are of different types and I think we should throw an exception or fall back to |
In my case the db user/pass are the same, only difference is the database name (this is a requirement for this feature ofcourse). I expected in this case an option to use the Shop model to find out the database name and form the query based on that. Might be too easy thought, but a feature in this direction could be interesting imo. |
I think you should add database prefix when defining |
@qiangxue you're right about database prefix and where to define it. |
Also it seems it should be a single connection in order to work properly. |
Overall it's kinda rare situation and framework has enough support for it. |
And then
|
based on http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#cross-database-relations, I would expect cross database joining to work without using examples like this #5470 (comment) |
@dynasource cross db JOIN is totally different from cross db relation. If you want to use join on a dbms that supports it, you need to include the database name in the table name of the record: public static function tableName()
{
return '{{databasename}}.{{tablename}}';
} implementing JOIN accross dbs on dbms that do not support it is not feasable in an efficient way. |
the insertion of that databasename in that tablename() can be quite cumbersome. For example: I've got a plain Mysql scenario in which
When I create i.e. an ActiveQuery in this public static function getDb()
{
return \Yii::$app->get('db_stats');
} Because of this, when I create a ActiveQuery from that It seems the framework is not (yet) smart enough to include the DB name for all other tables with the default |
Doing that automatically would introduce a lot of overhead to support a very rarely used feature. |
IMHO, it is not rare to have multiple DB's of the same DBMS wired to an app. An since we support it anyways, it would be logical to have this working out of the box. With respect to the overhead. That's another thing to be tackled, of course. |
/**
* @property StatsItem[] $statsItems
*/
class Item extends \yii\db\ActiveRecord
{
/**
* @return \yii\db\ActiveQuery
*/
public function getStatsItems()
{
return $this->hasMany(StatsItem::className(), ['stats_item_id' => 'id']);
}
} class StatsItem extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function getDb()
{
return \Yii::$app->get('db_stats');
}
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{stats}}.{{' . self::getDb()->getSchema()->getRawTableName(parent::tableName()) . '}}';
}
} $items = \common\models\Item::find()
->joinWith('statsItems')
->limit(20)
->all(); |
The problem is the other way around with StatsItem::find() |
@dynasource, I do not see a problem
|
the usecase is that Item returns a tableName like |
Yes, this was required by condition. Item returns a tableName like |
For MSSQL I had to do this:
But I will have to adapt my tests because, for simplicity, I have all tables in the same MySQL (not mssql) DB, not 20 dbs as we (unfortunately) have in prod server. A |
To automate this process in MySQL we can do smthng like this:
Profit - we don't need to hardcode db-name into the model, just to define it in config. Request in |
Closing due to inactivity. If this should be reopen please write here. |
Introduction
First of all; I'm not sure if this is not working by design or that it's a bug.
What I tried is making a join over two tables in separate databases with a WHERE statement on it.
The use case is that we have a second database with log tables and in the backend would like to join this with live data. I made a simplified test case.
I tried searching the forum + issues but couldn't find a similar issue.
Environment
Reproduce
Create two database configs in /config/web.php (with according settings):
Create database setup for database #1:
Then create setup for database #2:
Then we create two models, first Shop.php:
And Employee.php:
Now in a controller I would like to fetch some data with a criteria:
This results in an error:
The statement would work if the query is created like this:
The text was updated successfully, but these errors were encountered: