-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with listeningToModelEvents([ ]) #29
Comments
@dreamingmind which version of the Plugin are you using? Have you tried with the latest? |
Ach! I'll get back to you on this. Probably my error. |
O.K., I'll close it for the moment, feel free to re-open! |
My issue appears to be with associated tables failing to run configured events. I have a record protected function setDefaultTemplate(): void
{
$this->setDefaultData(function(Generator $faker) {
return [
// 'hash' => $faker->text(20), //this should be set by the event
'name' => $faker->text(15),
'code' => $faker->text(5),
];
})
->listeningToModelEvents(['Model.beforeSave']);
} This code produces all the required records but does not run the events for //In a test class
public function testItem()
{
ItemFactory::make()->standAloneItem()->persist();
}
//in the ItemFactory
public function standAloneItem($parameter = null, int $n = 1): ItemFactory
{
return $this
->withPrimarySku()
->with('Tenant.Warehouse');
}
public function withPrimarySku($parameter = null): ItemFactory
{
return $this->with(
'PrimarySku',
\App\Test\Factory\SkuFactory::make(['base_sku' => 1])
);
} If I call the This code for example will produce one public function testSku() {
$item = ItemFactory::make()->standAloneItem()->persist();
SkuFactory::make(['item_id' => $item->id, 'tenant_id' => $item->tenant->id])->persist();
} I'm currently in Cake 4.1.6 and FixtureFactory 2.1.0 |
I don't know how to re-open this issue. Should I just make a new one? |
@dreamingmind I see your issue. The event manager will apply events to the main factory, but not to its associations. Therefore, in I agree that this behavior of the package is wrong, since per default the SkuFactory listens to the beforeSave event. The event should be triggered. I suggest as a turn around that you explicitly enable the event in your ItemFactory as follows
Does this trigger the beforeSave event? I have re-opened the issue and will work in the coming days on a fix. |
@dreamingmind I have added a few tests to make sure that the model events were triggered in associations too. They are, including when the event is activated in the defautl template. You can have a look here: You can ignore my previous post. The error seems to be on your side. Which version of the plugin do you use? Do you use the latest v2.1.0 (v1.1.0 on Cake3)? |
Ok. I'm digging deeper. I've pulled your recent code and see the passing test. I'll see if I can either figure out what's wrong or write a failing test. |
@dreamingmind can I close this for now? |
Yes. I got sidetracked on another time sensitive task and will open a new issue if my research turns something up. The two specific details I will look into is the use of a static callable in the event handler and the triggering of an event that is more than 1 association away. Thanks |
Using the test code:
the event is failing to run.
In an attempt to find the problem, I looked at the
EventCollector
after thelisteningToModelEvents()
method ran and saw this:The last property of the
LocationFactory
readsprotected withModelEvents => false
.Based on this information I set
LocationFactory::withModelEvents
totrue
and the events ran properly. With this property set it does not matter whether I use the methodlisteningToModelEvents()
.Either the code is wrong or this section of the documentation is wrong.
The text was updated successfully, but these errors were encountered: