问题描述
这是一种来自
的后续问题。
有多个数据库连接:
return [
'default'=> 'mysql-key1',
'connections'=> [
'mysql-key1'=> [
'driver'=> 'mysql',
'database'=> 'key1_dbname,
// etc
],
'mysql-key2'=> [
'driver'=> 'mysql',
'database'=> 'key2_dbname',
// etc
]
]
];
我有一个产品存储库,使用模型setConnection来更改连接属性:
public function setConnection($ name){
// assume $ this-> product是您的产品模型
$ this - > product-> setConnection($ name);
}
但是,我发现它只适用于查询方法, Model :: create ::
$ productRepo = new ProductRepository
foreach($ array as $ key => $ value){
$ productRepo-> setConnection($ key。'_'。$ database);
// this works
$ products = $ productRepo-> all();
//这不工作,将使用旧的连接
$ product = $ productRepo-> create($ data);
}
似乎:: create方法不会解析连接已创建。问题是
setConnection()
在一个类的实例上工作,但 create()
方法是类本身的静态方法。在您的仓库中, $ this-> product
是Product类的一个实例。在执行查询之前,在此实例上使用 setConnection()
可以正常工作,但如果要对静态方法使用单独的连接,则需要执行一些更多的手动工作(例如 create()
)。
所有 create c $ c>方法会实例化一个具有给定属性的新实例,然后调用 save()
。因此,不是在产品模型上调用 create()
,只需手动执行:
class ProductRepository {
public function create(array $ attributes,$ connection = null){
$ product = $ this-> product-> newInstance属性);
$ product-> setConnection($ connection?:$ this-> product-> getConnectionName());
$ product-> save();
return $ product;
}
}
您还可以覆盖静态 create()
在产品模型上接受连接。
extends Model {
public static function create(array $ attributes,$ connection = null){
$ model = new static($ attributes);
$ model-> setConnection($ connection?:$ this-> connection);
$ model-> save();
return $ model;
}
}
类ProductRepository {
public function create(array $ attributes,$ connection = null){
$ connection = $ connection? $ this-> product-> getConnectionName()
return $ this-> product-> create($ attributes,$ connection);
}
}
This is sort of a follow up question from
Forcing Eloquent models to re resolve database connection
With multiple database connections :
return [
'default' => 'mysql-key1',
'connections' => [
'mysql-key1' => [
'driver' => 'mysql',
'database' => 'key1_dbname,
// etc
],
'mysql-key2' => [
'driver' => 'mysql',
'database' => 'key2_dbname',
// etc
]
]
];
I have a product repository which uses the Model setConnection to change the connection attribute :
public function setConnection($name) {
// assumes $this->product is your Product model
$this->product->setConnection($name);
}
However, i found out that it only worked on query methods and not methods like Model::create ::
$productRepo = new ProductRepository();
foreach ($array as $key => $value) {
$productRepo->setConnection($key . '_' . $database);
// this works
$products = $productRepo->all();
// this doesn't work and will use the old connection
$product = $productRepo->create($data);
}
Seems that the ::create method does not resolve the connection once the instance has been created. Anyone knows a fix?
解决方案
The problem is that setConnection()
works on an instance of a class, but the create()
method is a static method on the class itself. In your repository, $this->product
is an instance of the Product class. Using setConnection()
on this instance before doing queries will work fine, but you'll need to do a little more manual work if you want to use separate connections on the static methods (such as create()
).
All the create()
method does is instantiate a new instance with the given attributes and then call save()
. So, instead of calling create()
on the Product model, you'll just need to do this manually:
class ProductRepository {
public function create(array $attributes, $connection = null) {
$product = $this->product->newInstance($attributes);
$product->setConnection($connection ?: $this->product->getConnectionName());
$product->save();
return $product;
}
}
You could also override the static create()
method on the Product model to accept a connection, as well.
class Product extends Model {
public static function create(array $attributes, $connection = null) {
$model = new static($attributes);
$model->setConnection($connection ?: $this->connection);
$model->save();
return $model;
}
}
class ProductRepository {
public function create(array $attributes, $connection = null) {
$connection = $connection ?: $this->product->getConnectionName()
return $this->product->create($attributes, $connection);
}
}
这篇关于多租户在Laravel雄辩ORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!