jarektkaczyk / laravel-global-scope

Define Laravel Global Scopes easier.
http://softonsofa.com
MIT License
28 stars 3 forks source link

FatalErrorException in Application.php line 666: Maximum function nesting level of '100' reached, aborting! #1

Closed nonDeath closed 9 years ago

nonDeath commented 9 years ago

I have that error using last laravel 5.1 last update today.

I have a class that inherit from GlobalScope and a Trait for boot the scope

    /**
     * Boot the scope.
     *
     * @return void
     */
    public static function bootActivoTrait()
    {
        static::addGlobalScope(new ActivoScope);
    }

But when i referencing any method from the model inside the Scope it fall in the exception.

And i don't know if is a mistake or and issue to report. Please help me with this.

Regards!!

Sorry for my basic english XD

jarektkaczyk commented 9 years ago

@nonDeath Show me the code, it's hard to guess.

nonDeath commented 9 years ago

ActivoScope.php file.

namespace App\Models\Scopes;

use Sofa\GlobalScope\GlobalScope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class ActivoScope extends GlobalScope
{
    /**
     * Aplica el filtro global
     * @param  Buider $builder
     * @param  Model  $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        $column = $model->getQualifiedActivoColumn();
        $builder->where($column, '=', true);
    }

    // This method is just renamed from isPublishedConstraint
    public function isScopeConstraint(array $where, Model $model)
    {
        $column = $model->getQualifiedActivoColumn();

        return $where['type'] == 'Basic'&& $where['column'] == $column;
    }
}

ActivoTrait.php file:

namespace App\Models\Scopes;

use App\Models\Scopes\ActivoScope;

trait ActivoTrait
{
    /**
     * Boot the scope.
     *
     * @return void
     */
    public static function bootActivoTrait()
    {
        static::addGlobalScope(new ActivoScope);
    }

    /**
     * Devuelve el identificador completo de la columna del filtro global
     * @return string
     */
    public function getQualifiedActivoColumn()
    {
        return implode('.', [$this->getTable(), $this->getActivoColumn()]);
    }

    /**
     * Obtiene el nombre interno de la columna activo, puede tener otro nombre en el modelo
     * @return string
     */
    protected function getActivoColumn()
    {
        return (empty($this->activoColumn))? 'activo': $this->activoColumn;
    }
}

The Model's head, Translation trait do not need a boot method (just ignore it).

use App\Models\Scopes\ActivoTrait;

class Hotel extends Model implements TranslatabledContract
{
    use Translation, ActivoTrait;
...
}
nonDeath commented 9 years ago

@jarektkaczyk i change the method in the ActivoScope class

    public function apply(Builder $builder, Model $model)
    {
        $column = $model->getQualifiedActivoColumn();
        $builder->where($column, '=', true);
    }

for this

    public function apply(Builder $builder, Model $model)
    {
        $column = $builder->getModel()->getQualifiedActivoColumn();
        $builder->where($column, '=', true);
    }

And it work fine in all models thats uses the ActivoTrait trait

But, i don't know about the $model parameter usage causes the exception. When try to access a $model's method in the class ActivoScope for example $model->getTable() in the code above (the first) generates the same issue. But accessing by the $builder->getModel() (the second) the result is the expected and the exception don't fires.

Thanks for response my first question!

jarektkaczyk commented 9 years ago

@nonDeath In fact I have no idea why the apply and remove signatures were redefined in Laravel5 - before they looked like this apply(Builder $builder) and everything was working just the same.

I can't track the issue down now, but it's good that you got it working.