Symfony - 有没有办法将实体设置为全局?
Symfony - is there a way to set a entity global?
我有一个在登录页面上扩展的实体,但在下一页上该扩展名不存在(实体在两个页面上都存在)。有没有办法让我的扩展在下一页可用?
<?php
namespace RegistrationExtension\Subscriber;
use ...
class RegisterExtensionSubscriber implements EventSubscriberInterface{
private $company;
/**
* @var EntityRepositoryInterface
*/
private $mailTypeRepository;
/**
* @var EntityRepositoryInterface
*/
private $mailRepository;
/**
* @var EntityRepositoryInterface
*/
private $customerRepo;
public function __construct(
EntityRepositoryInterface $customerRepo,
EntityRepositoryInterface $mailTypeRepository,
EntityRepositoryInterface $mailRepository
) {
$this->customerRepo = $customerRepo;
$this->mailTypeRepository = $mailTypeRepository;
$this->mailRepository = $mailRepository;
}
public static function getSubscribedEvents(): array{
return [
AccountLoginPageLoadedEvent::class => 'extendCustomer',
AccountOverviewPageLoadedEvent::class => 'onCustomRegister'
];
}
此函数在登录页面完成加载时触发。
public function extendCustomer(AccountLoginPageLoadedEvent $event){
// if (isset($_POST['submit'])) {
if(isset($_GET["email"])){
$company = $_GET["email"];
}else{
$company = null;
}
$mailEntity = $this->mailTypeRepository->search(
(new Criteria())->addFilter(new EqualsFilter('mail_template_type.technicalName', "customer_register")),
$event->getContext()
);
if ($mailType = $mailEntity->getEntities()->first()) {
$mailEntity = $this->mailRepository->search(
(new Criteria())->addFilter(new EqualsFilter('mail_template.mailTemplateTypeId', $mailType->getId())),
$event->getContext()
);
$mailEntity->getEntities()->first()->addExtension('file_does_exist', new ArrayEntity(['company' => "blub"]));
dump($mailEntity->getEntities()->first());
}
// }
}
此函数在用户登录且页面完成加载后触发。
public function onCustomRegister(AccountOverviewPageLoadedEvent $event){
$mailEntity = $this->mailTypeRepository->search(
(new Criteria())->addFilter(new EqualsFilter('mail_template_type.technicalName', "customer_register")),
$event->getContext()
);
if ($mailType = $mailEntity->getEntities()->first()) {
$mailEntity = $this->mailRepository->search(
(new Criteria())->addFilter(new EqualsFilter('mail_template.mailTemplateTypeId', $mailType->getId())),
$event->getContext()
);
dump($mailEntity);
// $mail = $mailEntity->getEntities()->first()->addExtension('file_does_exist', new ArrayEntity(['company' => $company]));
}
$customerEntity = $this->customerRepo->search(
(new Criteria())->addFilter(new EqualsFilter('customer.customerNumber', $event->getSalesChannelContext()->getCustomer()->getCustomerNumber())),
$event->getContext()
);
}
}
难道是AccountOverviewPageLoadedEvent事件加载了beo?
正如您在开发过程中已经发现的那样,扩展不会持久化。
你想要的是 EntityExtension。这样您就可以扩展客户实体。但是,由于只能通过 EntityExtension 添加关联和外键字段,因此您需要一个自己的实体。
那么让我们假设您 create a entity:CustomerRegistrationFile
然后您可以扩展 CustomerDefinition 并将您的 CustomerRegistrationFile 添加为关联。通过这种方式,您可以直接在实体中持久保存数据,并可以在您需要的任何地方加载它,例如在您的邮件模板中。
旁注:关联不会自动加载。除了 OneToOneAssociations 可以从一端自动加载。不是两端的,以防止循环引用。
我有一个在登录页面上扩展的实体,但在下一页上该扩展名不存在(实体在两个页面上都存在)。有没有办法让我的扩展在下一页可用?
<?php
namespace RegistrationExtension\Subscriber;
use ...
class RegisterExtensionSubscriber implements EventSubscriberInterface{
private $company;
/**
* @var EntityRepositoryInterface
*/
private $mailTypeRepository;
/**
* @var EntityRepositoryInterface
*/
private $mailRepository;
/**
* @var EntityRepositoryInterface
*/
private $customerRepo;
public function __construct(
EntityRepositoryInterface $customerRepo,
EntityRepositoryInterface $mailTypeRepository,
EntityRepositoryInterface $mailRepository
) {
$this->customerRepo = $customerRepo;
$this->mailTypeRepository = $mailTypeRepository;
$this->mailRepository = $mailRepository;
}
public static function getSubscribedEvents(): array{
return [
AccountLoginPageLoadedEvent::class => 'extendCustomer',
AccountOverviewPageLoadedEvent::class => 'onCustomRegister'
];
}
此函数在登录页面完成加载时触发。
public function extendCustomer(AccountLoginPageLoadedEvent $event){
// if (isset($_POST['submit'])) {
if(isset($_GET["email"])){
$company = $_GET["email"];
}else{
$company = null;
}
$mailEntity = $this->mailTypeRepository->search(
(new Criteria())->addFilter(new EqualsFilter('mail_template_type.technicalName', "customer_register")),
$event->getContext()
);
if ($mailType = $mailEntity->getEntities()->first()) {
$mailEntity = $this->mailRepository->search(
(new Criteria())->addFilter(new EqualsFilter('mail_template.mailTemplateTypeId', $mailType->getId())),
$event->getContext()
);
$mailEntity->getEntities()->first()->addExtension('file_does_exist', new ArrayEntity(['company' => "blub"]));
dump($mailEntity->getEntities()->first());
}
// }
}
此函数在用户登录且页面完成加载后触发。
public function onCustomRegister(AccountOverviewPageLoadedEvent $event){
$mailEntity = $this->mailTypeRepository->search(
(new Criteria())->addFilter(new EqualsFilter('mail_template_type.technicalName', "customer_register")),
$event->getContext()
);
if ($mailType = $mailEntity->getEntities()->first()) {
$mailEntity = $this->mailRepository->search(
(new Criteria())->addFilter(new EqualsFilter('mail_template.mailTemplateTypeId', $mailType->getId())),
$event->getContext()
);
dump($mailEntity);
// $mail = $mailEntity->getEntities()->first()->addExtension('file_does_exist', new ArrayEntity(['company' => $company]));
}
$customerEntity = $this->customerRepo->search(
(new Criteria())->addFilter(new EqualsFilter('customer.customerNumber', $event->getSalesChannelContext()->getCustomer()->getCustomerNumber())),
$event->getContext()
);
}
}
难道是AccountOverviewPageLoadedEvent事件加载了beo?
正如您在开发过程中已经发现的那样,扩展不会持久化。 你想要的是 EntityExtension。这样您就可以扩展客户实体。但是,由于只能通过 EntityExtension 添加关联和外键字段,因此您需要一个自己的实体。
那么让我们假设您 create a entity:CustomerRegistrationFile 然后您可以扩展 CustomerDefinition 并将您的 CustomerRegistrationFile 添加为关联。通过这种方式,您可以直接在实体中持久保存数据,并可以在您需要的任何地方加载它,例如在您的邮件模板中。
旁注:关联不会自动加载。除了 OneToOneAssociations 可以从一端自动加载。不是两端的,以防止循环引用。