squire icon indicating copy to clipboard operation
squire copied to clipboard

Issue when using multiple models

Open ibrasho opened this issue 5 years ago • 1 comments

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

  • Squire version: 2.0.6
  • Laravel version: 8.33.1
  • Server OS: Ubuntu 18.04.4
  • PHP version: 8.0.1

ibrasho avatar Mar 23 '21 14:03 ibrasho

Fixed by #18.

danharrin avatar Apr 26 '21 21:04 danharrin