对此问题的解答已经解决: Symfony 4 and Doctrine, how to generate repository automatically after mapping?
我在从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”,但这不是有效的 类。检查您的班级命名。如果这是服务编号, 确保此服务存在并标记了 “ doctrine.repository_service”。
错误来自此部分:
$search = $entityManager -> getRepository(EmailComponents::class) -> findAll();
答案 0 :(得分:1)
我找到了解决方法:
我用过:
php bin\console make:entity --regenerate
生成缺少的类。
答案 1 :(得分:0)
我遇到了同样的问题,对我来说,这是因为我的entityRepository文件不是.php文件(但其中包含了php代码),我感到很愚蠢,因为颜色正确,phpstorm并没有帮助我。
我用Sinf说了php bin\console make:entity --regenerate
解决了这个问题
谢谢!