I created a custom entity with definition, collection, migration, etc. It is called tickets. I also created a customer extension to have a relation one too many
(One customer can have multiple tickets but the ticket can have only one owner - customer).
$collection->add(
(new OneToManyAssociationField(
'ticket',
TicketDefinition::class,
'customer_id',
))
->addFlags(new CascadeDelete())
);
Now I want to create a custom rule and everything goes well but how can I get the data from the custom collection? I can't inject a service into the rule and according to the documentation, I should stick with the data provided by scope. My question is how can I get those data in the match method?
public function match(RuleScope $scope): bool {
$scope->getSalesChannelContext()->getCustomer()->getExtensions()
}
returns null.
In the command, I can do a similar thing and when I do association with the criteria everything works but in the match method I can not do things like this.
I also checked and in the rest of the associations, there is an option autoload
but not in OneToMany
.
Please can you help me?
You need to manually add the association when the entities are loaded, as OneToManyAssociationField
does not support auto-loading. If you want to do this globally you can subscribe to EntitySearchedEvent
and add the association to the criteria. This should make the extension available globally.
class EntitySearchSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
EntitySearchedEvent::class => 'onEntitySearch',
];
}
public function onEntitySearch(EntitySearchedEvent $event): void
{
if ($event->getDefinition()->getEntityName() !== CustomerDefinition::ENTITY_NAME) {
return;
}
$event->getCriteria()->addAssociation('ticket');
}
}