laravel属于与不同数据库的关系不起作用 [英] laravel BelongsTo relationship with different databases not working

查看:73
本文介绍了laravel属于与不同数据库的关系不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在几个地方看到了远离"的地方,但是,可惜-这就是我的数据库的构建方式:

I've seen in several places to "stay away" from this, but alas - this is how my DB is built:

class Album extends Eloquent {

   // default connection

   public function genre() {
       return $this->belongsTo('genre');
   }

和流派表:

class Genre extends Eloquent {
    protected $connection = 'Resources';

}

我的database.php:

My database.php:

'Resources' => array(
                    'driver'    => 'mysql',
                    'host'      => 'localhost',
                    'database'  => 'resources',
                    'username'  => 'user',
                    'password'  => 'password',
                    'charset'   => 'utf8',
                    'collation' => 'utf8_unicode_ci',
                    'prefix'    => '',
            ),

 'mysql' => array(
                    'driver'    => 'mysql',
                    'host'      => 'localhost',
                    'database'  => 'my_data',
                    'username'  => 'user',
                    'password'  => 'password',
                    'charset'   => 'utf8',
                    'collation' => 'utf8_unicode_ci',
                    'prefix'    => '',
            ),

当我尝试运行时

Album::whereHas('genre', function ($q) {
   $q->where('genre', 'German HopScotch'); 
});

选择不正确(没有将数据库名称添加到类型"表中):

it doesn't select properly (doesn't add the database name to the table "genres"):

Next exception 'Illuminate\Database\QueryException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'my_data.genres' doesn't exist

重要的是要注意,这很完美:

Its important to note that this works perfectly:

Album::first()->genre;

更新

到目前为止,我发现最好的方法是使用构建器的"from"方法专门命名正确的连接. 我发现查询中的构建器可以接收来自"

The best I've found so far is to use the builder's "from" method to specifically name the correct connection. I've discovered that the builder inside the query can receive "from"

Album::whereHas('genre', function ($q) {
   $q->from('resources.genres')->where('genre', 'German HopScotch'); 
});

这是一个不错的解决方案,但它需要我深入数据库php并找到一种从关系体裁"中获取正确的表和数据库名称的好方法.

This is a decent solution but it requires me to dig in the database php and find a good way to get the proper table and database name from the relation 'genre'.

如果其他任何人都可以在此解决方案上构建并使其更通用,我将不胜感激.

I will appreciate if anyone else can build on this solution and make it more general.

推荐答案

这是我自己的解决方案,它通常对我有用,但是它非常复杂.

This is my own solution and it works in general for me but its mega-complicated.

我正在使用构建器"from"方法在子查询中正确设置表和数据库.我只需要在内部传递正确的信息即可.

I'm using the builder "from" method to set the table and database correctly inside the subquery. I just need to pass the correct information inside.

假设子查询可能像"genres.sample"一样复杂,甚至更深(这意味着专辑与类型有关,而类型与样本有关) 这就是方法

Assume the subquery can be as complicated as "genres.sample" or even deeper (which means albums has a relation to genres, and genres has a relation to samples) this is how

$subQuery = 'genres.samples';
$goDeep = (with (new Album));

$tableBreakdown =  preg_split('/\./', $subQuery); //  = ['genres', 'samples']

// I recurse to find the innermost table $album->genres()->getRelated()->sample()->getRelated()
foreach ($tableBreakdown as $table)
    $goDeep = $goDeep->$table()->getRelated();

// now I have the innermost, get table name and database name

$alternativeConnection =  Config::get("database.connections." . $goDeep->getConnectionName() . ".database"); // should be equal to the correct database name

$tableName = $goDeep->getTable(); // I have to use the table name in the "from" method below

Album::whereHas($subQuery, function ($q) use ($alternativeConnection, $tableName) {
$q->from("$alternativeConnection.$tableName"); 
$q->where(....... yadda yadda);
    });

tl:dr;

Album::whereHas('genres', function ($q) { 
    $q->from('resources.genres')->where(....); 
});

这篇关于laravel属于与不同数据库的关系不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆