Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Maximum function nesting level of '512' reached, aborting! #119

Closed
jetwes opened this issue Jan 31, 2019 · 15 comments
Closed

Maximum function nesting level of '512' reached, aborting! #119

jetwes opened this issue Jan 31, 2019 · 15 comments

Comments

@jetwes
Copy link

jetwes commented Jan 31, 2019

I set up the event-projector in my local environment - just for one model for testing purposes. When i create a new entry in tinker everything works fine. However - if i try it in my application i get this error:
Maximum function nesting level of '512' reached, aborting!

image

Any clues?

@sebastianmulders
Copy link
Contributor

sebastianmulders commented Jan 31, 2019

What type of attributes does your storable event have? I've run into a similar issue when passing a full Carbon instance. You need to serialize those kind of attributes, in case of Carbon simply by using toDateTimeString() instead of the whole instance.

@jetwes
Copy link
Author

jetwes commented Jan 31, 2019

Wow - that did the trick. Ok - that's going to be hard for me, if i really want to use the projector on all my models...

@jetwes jetwes closed this as completed Jan 31, 2019
@jetwes
Copy link
Author

jetwes commented Jan 31, 2019

Any clue why this is happening? I saw in the source of StoredEvent that there is a full Carbon instance, too...
$storedEvent->created_at = Carbon::now();

In my Model i store a "date_of_absence" which is a Carbon instane...

@sebastianmulders
Copy link
Contributor

That's likely because your date_of_absence is not listed in your Model's protected $dates. See: https://laravel.com/docs/5.7/eloquent-mutators#date-mutators

@jetwes
Copy link
Author

jetwes commented Jan 31, 2019

It is listed. I think the problem is that $dates isn't present in Spatie\EventProjector\Models\StoredEvent.
So it's impossible for the class to determine Carbon instances.
image

@sebastianmulders
Copy link
Contributor

Well, Spatie's StoredEvent model extends the default Eloquent model, so in theory, that should work. You could also create your own custom StoredEvent model.

@jetwes
Copy link
Author

jetwes commented Jan 31, 2019

My PortalBaseModel extends Elqouent Model and adds a Cachable Trait... Shouldn't be a problem.

@jetwes
Copy link
Author

jetwes commented Jan 31, 2019

Tried it without my BaseModel - same result..

@jetwes
Copy link
Author

jetwes commented Jan 31, 2019

It doesn't even matter if you cast a date as date:Y-m-d ...

@sebastianmulders
Copy link
Contributor

I'm not sure where things go wrong for you. With a date column present in $dates in one of our models, we can use now() or Carbon::now() to set a value for that particular column. The event appears in the stored_events table as it should.

Without that column being present in $dates, a maximum function level error occurs. That is because the column isn't casted properly, and thus, the model serializer gets stuck while serializing that full Carbon instance.

There's a difference between casting dates in the StoredEvent model and models that get serialized for the event_properties column in it (e.g. your Absence model).

@jetwes
Copy link
Author

jetwes commented Feb 6, 2019

I really don't know why it's not working on my side...

My Model:

<?php

namespace Portal\Models;

use Illuminate\Database\Eloquent\Model;
use Portal\Events\AbsenceCreated;
use Portal\Events\AbsenceDeleted;
use Ramsey\Uuid\Uuid;

class Absence extends Model
{
    protected $table = 'absences';
    protected $guarded = [];
    protected $dates = ['date_start','date_end','date_of_absence'];
    /*protected $casts = [
        'date_of_absence' => 'date:Y-m-d',
    ];*/

    public static function createWithAttributes(array $attributes): Absence
    {
        /*
         * Let's generate a uuid.
         */
        //dd($attributes);
        $attributes['uuid'] = (string) Uuid::uuid4();

        /*
         * The account will be created inside this event using the generated uuid.
         */
        event(new AbsenceCreated($attributes));

        /*
         * The uuid will be used the retrieve the created account.
         */
        return static::uuid($attributes['uuid']);
    }

    public function remove()
    {
        event(new AbsenceDeleted($this->uuid));
    }

    public static function uuid(string $uuid): ?Absence
    {
        return static::where('uuid', $uuid)->first();
    }
}

The Projector:


class AbsenceProjector implements Projector
{
    use ProjectsEvents;

    /*
     * Here you can specify which event should trigger which method.
     */
    protected $handlesEvents = [
        AbsenceCreated::class => 'onAbsenceCreated',
        AbsenceDeleted::class => 'onAbsenceDeleted',
    ];

    public function onAbsenceCreated(AbsenceCreated $event)
    {
        Absence::create($event->absenceAttributes);
    }

    public function onAbsenceDeleted(AbsenceDeleted $event)
    {
        Absence::uuid($event->absenceUuid)->delete();
    }
}

The Event:


class AbsenceCreated implements ShouldBeStored
{
    /** @var array */
    public $absenceAttributes;

    public function __construct(array $absenceAttributes)
    {
        $this->absenceAttributes = $absenceAttributes;
    }
}

In MY Controller:
```
$date_start = Carbon::createFromFormat('Y-m-d',$request->get('date_start'));
$date_end = Carbon::createFromFormat('Y-m-d',$request->get('date_end'));

    $date_start2 = clone $date_start;
    //parse through days of month
    $dates = [];
    while ($date_start->lte($date_end)) {
        $dates[] = $date_start->copy();
        $date_start->addDay();
    }

foreach ($dates as $date) {
..... some other logic....
if (Absence::createWithAttributes([
'user_id' => $request->get('user_id'),
'date_of_absence' => $date, // it work's with $date->format('Y-m-d')
'date_start' => $date_start2->format('Y-m-d'),
'date_end' => $date_end->format('Y-m-d'),
'absence_type' => $request->get('absence_type'),
'comment' => $request->get('comment'),
'state' => $approved,
'approver' => $approver
])) $ok = true;
}

@enricobono
Copy link

Hi jetwes, did you solve the problem?
I'm running in a similar problem with dates columns...

@sebastianmulders
Copy link
Contributor

I'd be happy to take a look at it if either one of you could set up a demo repo.

@jetwes
Copy link
Author

jetwes commented Mar 11, 2019

Unfortunately i hadn't the time in the last weeks to investiagte further - so i stopped using the package for the moment...

@enricobono
Copy link

I'd be happy to take a look at it if either one of you could set up a demo repo.

I guess the problem is in the serialization/deserialization. I opened another issue here: #126
but I guess it's all the same story

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants