Laravel雄辩ORM多租户

Laravel雄辩ORM多租户

本文介绍了Laravel雄辩ORM多租户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从

使用多个数据库连接:

return [
    'default' => 'mysql-key1',
    'connections' => [
        'mysql-key1' => [
            'driver'    => 'mysql',
            'database'  => 'key1_dbname,
            // etc
        ],
        'mysql-key2' => [
            'driver'    => 'mysql',
            'database'  => 'key2_dbname',
            // etc
        ]
    ]
];

我有一个使用Model setConnection更改连接属性的产品库:

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);
}

但是,我发现它只适用于查询方法,而不是像Model :: create ::

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);
}

似乎:: create方法一旦实例没有解析连接已创建任何人都知道修复?

Seems that the ::create method does not resolve the connection once the instance has been created. Anyone knows a fix?

推荐答案

问题是 setConnection()在类的实例上工作,但 create()方法是类本身的静态方法。在您的资料库中, $ this-> product 是Product类的一个实例。在执行查询之前,在此实例上使用 setConnection()可以正常工作,但如果要在静态方法上使用单独的连接,则需要进行更多的手动工作(例如 create())。

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()).

所有的 create()方法实例化具有给定属性的新实例,然后调用 save()。因此,您不需要在产品型号上调用 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;
    }
}

您还可以覆盖静态 create()方法,以接受连接。

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多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 17:48