The problem
As per my previous question here, it was pointed out to me that I shouldn't be trying to fill related models in a Laravel Factory (i.e I should fill them in their own factory).
However, I have an observer that looks for the related data during the creation and tries to fill related models (this is so I can create multiple related entities using just the create::
method and a single multistep form). Now, I need to add a check to see if this data is populated in the observer so I don't have to specify it in the factory.
In doing so, I now get a segmentation fault when trying to seed my database. I've narrowed down the cause to this line - without the isset
check, it works fine (other than $data['day'] is not specified, hence the check);
Segmentation fault (core dumped)
if(isset($data['day'])) $event->day->fill($data['day']);
Related Code
EventFactory.php
$factory->define(App\Event::class, function (Faker $faker) {
return [
"name" => "A Test Event",
"description" => $faker->paragraphs(3, true),
"event_start_date" => today(),
"event_opening_date" => today(),
"event_closing_date" => tomorrow(),
"user_id" => 1,
"banner_id" => 1,
"gallery_id" => 1,
"related_event_id" => 1,
"status" => "published",
"purchase_limit" => 1000,
"limit_remaining" => 1000,
"delivery_method" => "collection",
"merchandise_delivery_method" => "collection"
];
});
EventObserver.php
public function created($event){
# get all attributes
$data = $event->getAttributes();
# fill any related models
if(isset($data['day'])) $event->day->fill($data['day']);
# save user
$event->push();
}
public function updating($model){
# get all attributes
$data = $model->getAttributes();
# fill any related models
if(isset($data['day'])) $model->day->fill($data['day']);
# save user
$model->push();
}
Other Info
Command: sudo php artisan migrate:reset --seed
Host: Windows 10
VM Environment: Vagrant running Ubuntu 16.04 via HyperV, mounted share with Samba
PHP Version: 7.1.20
Laravel Version: 5.7
Turns out the issue is actually with this line;
$event->push();
Could there be something recursive happening here?
With Namoshek's help, I can now narrow it down to the following error from xdebug;
Maximum function nesting level of '256' reached, aborting!
Increasing xdebug.max_nesting_level to 200000 brings back the segfault.
This seems to me like it's stuck in an infinite loop. However, I can't see how calling save()
or push()
in created
would end up calling back to itself. Confused.
This did indeed turn out to be an infinite recursion issue. Eliminating the line:
$event->push(); // this line appears to call update again, which in turn calls push, which calls update etc...
Solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With