php - 以表格形式显示数据库的信息以持久保存在另一个数据库中 Php-Symfony
问题描述
我有 2 个数据库默认数据库和客户数据库两次配置良好doctrine.yaml
!Article
我想在表单上显示来自客户数据库的表信息,以填充默认数据库的实体 Demandes 并在之后保留它。这两个实体通过外键(idArticle
在 Demandes 中)链接,但如有必要,我可以取消它。
但问题是我总是有错误
给定类型为“App\Entity\Main\Article 或 null”、“App\Entity\Customer\Article 的实例”的预期参数。或其他错误,如 The class 'App\Entity\Main\Demandes' was not found in the chain configured namespaces App\Entity\Customer
我该怎么做才能理解我只想用信息文章填充实体 Demandes?
->add('idArticle', EntityType::class, [
'class' => 'App\Entity\Customer\Article',
'mapped' => true,
'em' => $options['customer_entity_manager'],
'choice_label' => 'idArticle'
])
还有我的控制器
$customerEntityManager = $this->getDoctrine()->getManager('customer');
$demande = new Demandes();
$demande->setTypeDai("Article");
$demande->setDate(new \DateTime());
$form = $this->createForm(DemandesTypeArticles::class, $demande, [
'customer_entity_manager' => $customerEntityManager
]);
$form->handleRequest($request);
dump($demande);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager('default');
$entityManager->persist($demande);
$entityManager->flush();
}
解决方案
您可以使用ChoiceType::class
如下代码所示:
class DemandesTypeArticles extends AbstractType {
/**
* @var EntityManager
*/
private $em;
public function __construct(EntityManagerInterface $em) {
$this->em = $em; // Pass 'EntityManager' as Service argument.
}
// ...
public function buildForm(FormBuilderInterface $builder, array $options): void {
$articles = $this->em->getRepository(Article::Class)->findAll();
$arctileChoices = [];
foreach($articles as $article) {
$label = 'Title: '.$article->getTitle().'; Id: '.$article->getId(); // Change how article choice label showed get rendert.
$arctileChoices[$label] = $article->getId();
}
$builder
// ...
->add('idArticle', ChoiceType::class, [
'mapped' => true, // Is true by default.
'choices' => $articleChoices,
])
;
}
// ...configureOptions and stuff
}
推荐阅读
- java - 移除回调后如何重用Android-Handler
- excel - 如何在 Excel VBA 中比较两个锯齿状数组
- c# - ValidationAttribute 注入服务,用于不显眼的客户端验证
- java - java中是否有一个函数可以帮助格式化并返回格式化的日期作为输出?
- java - findById() 不适用于 Spring Boot 和 Jpa
- wordpress - NGINX - 匹配所有位置
- c# - 如何在 DataGrid.ContextMenu MVVM 中绑定来自两个源(Datagrid 和 TreeView)的多个 CommandParameter
- postgresql - 如何打开docker卷?
- sql - 有没有办法在没有 Xampp 的情况下使用 phpmyadmin,或者在没有 xampp 的情况下访问数据库之类的东西
- python - 为什么使用 struct 打包生存时间(ttl)?