torifat/eloquent-single-table-inheritance

View on GitHub
src/SingleTableInheritanceTrait.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Rifat\EloquentSingleTableInheritance;


trait SingleTableInheritanceTrait
{

    /**
     * The ClassName where this Trait was invoked
     *
     * @var string
     */
    protected static $_stiBaseClass = __CLASS__;

    /**
     * The Database field for storing the ClassName for STI (Single Table Inheritance)
     *
     * @var string
     */
    protected static $stiClassNameField = 'class_name';

    /**
     * Indicates if the query on Parent Model should return the Children Model results
     *
     * @var bool
     */
    protected static $strictMode = false;

    /**
     * Get the table associated with the model.
     *
     * @return string
     */
    public function getTable()
    {
        if (isset($this->table)) return $this->table;

        // Setting the value so that it doesn't evaluate each time
        $this->table = str_replace('\\', '', snake_case(str_plural(class_basename(static::$_stiBaseClass))));

        return $this->table;
    }

    /**
     * Get a new query builder for the model's table.
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function newQuery()
    {
        $builder = parent::newQuery();
        $className = get_class($this);
        if (static::$strictMode || $className !== static::$_stiBaseClass) {
            $builder->where(static::$stiClassNameField, '=', $className);
        }
        return $builder;
    }

    /**
     * Save the model to the database.
     *
     * @param  array $options
     * @return bool
     */
    public function save(array $options = array())
    {
        $this->setAttribute(static::$stiClassNameField, get_class($this));
        parent::save($options);
    }

    /**
     * Create a new model instance that is existing.
     *
     * @param  array $attributes
     * @return static
     */
    public function newFromBuilder($attributes = array())
    {
        $class = $attributes->{static::$stiClassNameField};
        $instance = new $class;
        $instance->exists = true;
        $instance->setRawAttributes((array)$attributes, true);
        return $instance;

    }

}