Thx to this answer问题已经解决:Symfony 4和Doctrine,如何在映射后自动生成存储库?
我在从 MySQL 表中获取数据时遇到问题,
这是我的控制器:
namespace App\Controller\Admin;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use App\Entity\EmailComponents;
class MigrationController extends AbstractController
{
/**
* @Route("/migration", name="migration_script")
*/
public function index(Request $request)
{
$entityManager = $this -> getDoctrine() -> getManager();
$email = new EmailComponents();
$form = $this->createFormBuilder($email)
->add('language', ChoiceType::class, [
'choices' => [
'Anglais' => 'en',
'Français' => 'fr',
'Allemend' => 'de',
'Néerlandais' => 'nl',
],
])
->add('subject', TextType::class)
->add('body', TextareaType::class)
->add('save', SubmitType::class, ['label' => 'Crée Email'])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$email = $form->getData();
$entityManager -> persist($email);
$entityManager -> flush();
$this->addFlash('success', 'Email a été crée avec succès');
return $this -> redirectToRoute('migration_script');
}
$search = $entityManager -> getRepository(EmailComponents::class) -> findAll();
return $this->render('admin/migration/index.html.twig', array(
'form' => $form -> createView()
));
}
这是 EmailComponents.php 文件:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\EmailComponentRepository")
*/
class EmailComponents
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=2)
*/
private $language;
/**
* @ORM\Column(type="string", length=250)
*/
private $subject;
/**
* @ORM\Column(type="string", length=5000)
*/
private $body;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getLanguage()
{
return $this->language;
}
public function setLanguage($language)
{
$this->language = $language;
}
public function getSubject()
{
return $this->subject;
}
public function setSubject($subject)
{
$this->subject = $subject;
}
public function getBody()
{
return $this->body;
}
public function setBody($body)
{
$this->body = $body;
}
}
问题是每次运行页面时,都会收到以下错误消息:
“App\Entity\EmailComponents”实体的 repositoryClass 设置为“App\Repository\EmailComponentRepository”,但这不是有效的类。检查您的类命名。如果这是一个服务 ID,请确保此服务存在并标记为“doctrine.repository_service”。
错误来自这部分:
$search = $entityManager -> getRepository(EmailComponents::class) -> findAll();
我找到了解决方案:
我用了 :
php bin\console make:entity --regenerate
生成缺失的类。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句