Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 4 PHP例外原则\ORM\ORMException:“;未知实体名称空间ali为';用户'&引用;_Php_Symfony - Fatal编程技术网

Symfony 4 PHP例外原则\ORM\ORMException:“;未知实体名称空间ali为';用户'&引用;

Symfony 4 PHP例外原则\ORM\ORMException:“;未知实体名称空间ali为';用户'&引用;,php,symfony,Php,Symfony,这是我的订户类。我想让用户给他发一封电子邮件。 我在这里使用EntityManagerInterface 使用条令\ORM\entitymanager界面; 最终类RegisterMailSubscriber实现EventSubscriberInterface { 私人$mailer; 公共函数构造(\Swift\u Mailer$Mailer,entityManager接口$entityManager) { $this->mailer=$mailer; $this->repository=$e

这是我的订户类。我想让用户给他发一封电子邮件。 我在这里使用EntityManagerInterface

使用条令\ORM\entitymanager界面;
最终类RegisterMailSubscriber实现EventSubscriberInterface
{
私人$mailer;
公共函数构造(\Swift\u Mailer$Mailer,entityManager接口$entityManager)
{
$this->mailer=$mailer;
$this->repository=$entityManager->getRepository('AppEntity:User');
}
公共静态函数getSubscribedEvents()
{
返回[
KernelEvents::VIEW=>['sendMail',EventPriorities::POST_WRITE],
];
}
公共函数sendMail(ViewEvent$event):无效
{
$user=$event->getControllerResult();
$method=$event->getRequest()->getMethod();
if(!$user instanceof user | | Request::METHOD_POST!==$METHOD){
返回;
}
$userInfo=$this->repository->find($user->getId());
}
}

您需要导入所有用作User、Request、ViewEvent、KernelEvent等的依赖项

顺便说一句,导入存储库(UserRepository)而不是entityManager是一种很好的做法,但是您不需要它,因为您已经有$user了。你不需要再找到它

我认为,如果在这些名称空间(位置)中有用户类,这就足够了:


您可能需要一些
use
语句,例如
use-App\Entity\User
,可能还需要一些
Request
的语句,首先将名称空间添加到类中(例如
namespace-App\EventSubcriber
),也可以使用完全限定的类名替换
$entityManager->getRepository('AppEntity:User')
(例如
$entityManager->getRepository(User::class)
)。User::class创建App\Entity\User not AppEntity:User,如果我没有弄错的话。也许它会有用。您在这个文件上设置了正确的命名空间吗?谢谢@AythaNzt。这很有帮助
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\User;

final class RegisterMailSubscriber implements EventSubscriberInterface
{
    private $mailer;

    public function __construct(\Swift_Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::VIEW => ['sendMail', EventPriorities::POST_WRITE],
        ];
    }

    public function sendMail(ViewEvent $event): void
    {
        $user = $event->getControllerResult(); 
        $method = $event->getRequest()->getMethod();

        if (!$user instanceof User || Request::METHOD_POST !== $method) {
            return;
        }

       $userEmail = $user->getEmail(); //for example. You got the user 5 lines before.
        
    }
}