laravel連接多個不同數據庫的單例類

PHP中文网
Lepaskan: 2023-03-11 09:20:02
asal
1211 orang telah melayarinya

在連接多個不同數據庫時,需要寫多個連接,爲了簡化該操作,可以使用該基類,不同的數據庫只要建立好相對應的類繼承該類,就可以使用ORM模型進行操作了。

<code class="sourceCode php"><span class="kw">class</span> singletonInstance 
{

    <span class="kw">protected</span> <span class="kw">static</span> <span class="kw">$instances</span> = <span class="ot">[];</span>
    <span class="kw">protected</span> <span class="kw">static</span> <span class="kw">$table</span> = <span class="kw">null</span><span class="ot">;</span>
    <span class="kw">protected</span> <span class="kw">static</span> <span class="kw">$connection</span> = <span class="kw">null</span><span class="ot">;</span>

    <span class="kw">public</span> <span class="kw">function</span> <span class="fu">__construct</span><span class="ot">()</span>
    {
        <span class="kw">if</span><span class="ot">(</span>!<span class="fu">isset</span><span class="ot">(</span><span class="kw">static</span>::<span class="kw">$instances</span><span class="ot">[</span><span class="kw">static</span>::<span class="kw">class</span><span class="ot">]))</span>{
            <span class="kw">if</span><span class="ot">(</span><span class="fu">is_null</span><span class="ot">(</span><span class="kw">self</span>::<span class="kw">$table</span><span class="ot">)</span> || <span class="fu">is_null</span><span class="ot">(</span><span class="kw">self</span>::<span class="kw">$connection</span><span class="ot">))</span>
            {
                <span class="kw">throw</span> <span class="kw">new</span> \<span class="kw">Exception</span><span class="ot">(</span><span class="st">'not defined property'</span><span class="ot">);</span>
            }
            <span class="kw">static</span>::<span class="kw">$instances</span><span class="ot">[</span><span class="kw">static</span>::<span class="kw">class</span><span class="ot">]</span> = <span class="kw">DB</span>::connection<span class="ot">(</span><span class="kw">self</span>::connection<span class="ot">)</span>->table<span class="ot">(</span><span class="kw">self</span>::<span class="kw">$table</span><span class="ot">);</span>
        }
    }

    <span class="kw">public</span> <span class="kw">static</span> <span class="kw">function</span> getDB<span class="ot">()</span>
    {
        <span class="kw">return</span> <span class="kw">static</span>::<span class="kw">$instances</span><span class="ot">[</span><span class="kw">static</span>::<span class="kw">class</span><span class="ot">];</span>
    }

}</code>
Salin selepas log masuk

Atas ialah kandungan terperinci laravel連接多個不同數據庫的單例類. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan