Har*_*han 5 eloquent jenssegers-mongodb laravel-5.3
我有两个模型,一个是扩展 Jenssegers\Model 的 mongo 模型,另一个是扩展 Illuminate\Model 的 sql 模型。此 SQL 模型没有定义连接名称,因为我们有多个数据库连接,每个数据库中都有相同的表。
Mongo模型注释.php
<?php
namespace App\Models\Mongo;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Comment extends Eloquent
{
/** @var string Mongo Connection Name */
protected $connection = 'mongodb';
/** @var string Mongo Collection Name */
protected $collection = 'comments';
/** @var bool Enable/Disable Timestamp */
public $timestamps = true;
/** @var date Date format */
protected $dateFormat = 'Y-m-d H:i:s';
public function userProfile()
{
return $this->belongsTo('\\App\\Models\\Sql\\UserDefaultProfile', 'created_by', 'user_code');
}
}
Run Code Online (Sandbox Code Playgroud)
Sql 模型 UserProfile.php
<?php
namespace App\Models\Sql;
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
class UserDefaultProfile extends Model
{
use HybridRelations;
/** @var string Table Name */
protected $table = 'user_default_profile';
/** @var bool Enable/Disable Timestamp */
public $timestamps = false;
}
Run Code Online (Sandbox Code Playgroud)
我有多个数据库连接添加到 Capsule
try {
$getDatabaseList = StoreDatabaseCredential::all();
} catch (Exception $exception) {
}
foreach ($getDatabaseList as $database) {
if (strtolower($database->database_type) == 'mysql') {
$db->addConnection([
'driver' => 'mysql',
'host' => $database->database_router_read_host,
'port' => $database->database_router_read_port,
'database' => $database->database_name,
'username' => $database->database_user,
'password' => $database->database_password,
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
], $database->connection_name);
}
}
Run Code Online (Sandbox Code Playgroud)
所以多个数据库连接可用
现在的问题是,当我用关系来称呼雄辩者时,我得到了Database [Default] not configured。我收到此错误是因为 UserProfile Model 没有定义任何连接。因此,请有人知道如何将连接名称传递给关系模型。
try {
$comments = Comment::where([
'in_reply_to_content_id' => $contentId,
'super_store_id' => $superStoreId,
'is_deleted' => 0
])->with([
'userProfile' => function ($query) use ($dbConnectionName) {
$query->select('id', 'user_code', 'mobile', 'name', 'profile_pic_url');
}
])->skip($offset)->take($itemsPerPage)->orderBy('created_at', 'desc')->get();
Utils::printData($contentComments);
exit();
} catch (\Throwable $exception) {
Utils::printData($exception->getMessage());
exit();
}
Run Code Online (Sandbox Code Playgroud)
那么,是否可以对关系做这样的事情
with([
'userProfile' => function ($query) use ($dbConnectionName) {
$query->setConnection($dbConnectionName)->select(
'id', 'user_code', 'mobile', 'name', 'profile_pic_url'
);
}
])->skip($offset)->take($itemsPerPage)->orderBy('created_at', 'desc')->get();
Run Code Online (Sandbox Code Playgroud)
您可以扩展模型Model的基类UserProfile,重写getConnectionName方法并从使用方法static设置的类变量中获取连接名称 static
例如:
use Illuminate\Database\Eloquent\Model;
class RelationWithChangingConnection extends Model
{
static $connectionName;
static public function setConnectionNameForRelation($connectionName)
{
static::$connectionName = $connectionName;
}
public function getConnectionName()
{
$this->connection = static::$connectionName;
return parent::getConnectionName();
}
}
class UserDefaultProfile extends RelationWithChangingConnection
{
//...
}
Run Code Online (Sandbox Code Playgroud)
进而
with([
'userProfile' => function ($query) use ($dbConnectionName) {
UserDefaultProfile::setConnectionNameForRelation($dbConnectionName);
$query->setConnection($dbConnectionName)->select(
'id', 'user_code', 'mobile', 'name', 'profile_pic_url'
);
}
])...
Run Code Online (Sandbox Code Playgroud)
然而,这看起来很难看。您还可以按照此处的建议通过切换配置文件来切换数据库连接
UPD:在这里我遇到了另一个可能的解决方案,但尚未检查它 - 您可以像这样编写关系子查询:
...
->with([
'userProfile' => function ($query) use ($dbConnectionName) {
$query->from('[yourdatabase].[dbtable]')->select('id','user_code', 'mobile', 'name', 'profile_pic_url');
}
...
Run Code Online (Sandbox Code Playgroud)
它将连接到您设置的数据库和数据库表。
| 归档时间: |
|
| 查看次数: |
8872 次 |
| 最近记录: |