Coder Social home page Coder Social logo

Comments (3)

joshbruce avatar joshbruce commented on July 25, 2024

I made the following, which seems to work:

class Base extends Model
{
    public static function allQuery($columns = ['*'])
    {
        if (!in_array(static::class, static::$singleTableSubclasses)) {
            $query = null;
            foreach(static::$singleTableSubclasses as $subClass) {
                if (is_null($query)) {
                    $query = static::where('type', $subClass::$singleTableType);

                } else {
                    $query->orWhere('type', $subClass::$singleTableType);

                }
            }
            return $query;
        }
        return static::where('type', static::$singleTableType);
    }

    public static function all($columns = ['*']) 
    {
        return static::allQuery($columns)->get();
    }
}

This allows you to do something like:

Base::all()

/*
\Collection {
    all: [
        \Sub { }
        \SubAlt { }
    ]
}
*/

Sub::all()
/*
\Collection {
    all: [
        \Sub { }
    ]
}
*/

SubAlt::all()
/*
\Collection {
    all: [
        \SubAlt { }
    ]
}
*/

Believe this could be added to the trait itself and accomplish the same goal, but I'm not sure it's the most elegant solution.

from single-table-inheritance.

joshbruce avatar joshbruce commented on July 25, 2024

Second iteration:

  • Uses DB.
  • Allows columns specification.
  • Cleaner implementation overall.
  • Takes better advantage of developer settable properties.
    public static function query($columns = ['*'])
    {
        $obj = new static();
        $query = DB::table($obj->table)->select($columns);
        if (in_array(static::class, static::$singleTableSubclasses)) {
            $query = $query->where(static::$singleTableTypeField, static::$singleTableType);
        }
        return $query;
    }

    public static function all($columns = ['*']) 
    {
        return static::query($columns)->get();
    }

Going to be adding this to my own trait internal to my project. I have a mental note to submit a PR, but cannot promise timing and want to make sure I'm covering as many use cases as possible using my own project as the guinea pig first.

from single-table-inheritance.

jonspalmer avatar jonspalmer commented on July 25, 2024

I can't reproduce the error you describe and have added test that proves it works today dd0668b.

from single-table-inheritance.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.