squirephp / squire

A library of static Eloquent models for common fixture data.
MIT License
1k stars 69 forks source link

Issue when using multiple models #17

Closed ibrasho closed 3 years ago

ibrasho commented 3 years ago

Describe the bug Every squire model overrides the $sqliteConnection property when it's booted.

Every model ends up with the database path of the last model that loaded.

To reproduce

new Squire\Models\Country;

new Squire\Models\Currency;

Squire\Models\Country::all()

Will throw an exception, since $sqliteConnection is set to the Currency model connection.

Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1 no such table: countries (SQL: select * from "countries")'

This is a sample PHP code to show the logic in action:

<?php
class A {
    protected static $v;
    public static function set($v) { static::$v = $v; }
    public static function get() { return static::$v; }
}

class B extends A {}
class C extends A {}

B::set('B');
C::set('C');

echo B::get();
echo C::get();

Will output two CC instead of the expected BC.

The same behavior is impacting the static $sqliteConnection property in all classes that extend Squire\Model.

The best solution is to set the $sqliteConnection in every model class, or to convert it to a map.

Expected behavior

Separate models should have different database connections.

Context

danharrin commented 3 years ago

Fixed by #18.