EDIT
Forgot to include the package i am using: https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html
I am trying to figure out how i can pass extra data to the factory to use afer persist in some custom code.
I have a factory that generates a University, and after that is inserted in the database i need to then use the Course factory to assign courses which i want to do in afterPersist.
I have tried to add my own method to set the array of courses on the object:
/** * @param array $courses * @return $this */ public function setCourses(array $courses) : self { $this->courses = $courses; return $this; }
And then call it like this:
foreach($this->universities as $university){ UniversityFactory::new()->setCourses($university['courses'])->create(['name'=>$university['name'],'slug'=>$university['slug'] ]); }
However, i end up with an empty array in the afterPersists (this->courses is empty):
return $this->afterPersist(function(University $university){ foreach($this->courses as $type => $courseName){ CourseFactory::new()->create(['name'=>$courseName,'type'=>$type,'uni'=>$university ]); } });
The array of courses does get assigned, but when running create() it gets emptied.