您好,当我尝试从数据库中删除某些内容时,找不到错误。
它说我的命名空间有误,但是我真的找不到任何问题。
这是我正在呼叫的动作
/**
* @Route("/delete/{id}", name="delete")
* @param $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function deleteById($id)
{
$em = $this->getDoctrine()->getManager();
$entries = $em->getRepository(Database_InteractionType::class)->find($id);
$em->remove($entries);
$em->flush();
return $this->redirectToRoute('show');
}
这是实体和我的表格。 我已经连续观看了2个小时,但似乎找不到Iusse。.
实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\Database_InteractionRepository")
*/
class Database_Interaction
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $question;
/**
* @ORM\Column(type="string", length=255)
*/
private $answer;
public function getId(): ?int
{
return $this->id;
}
public function getQuestion(): ?string
{
return $this->question;
}
public function setQuestion(string $question): self
{
$this->question = $question;
return $this;
}
public function getAnswer(): ?string
{
return $this->answer;
}
public function setAnswer(string $answer): self
{
$this->answer = $answer;
return $this;
}
}
我的表格:
<?php
namespace App\Form;
use App\Entity\Database_Interaction;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class Database_InteractionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'question',
TextType::class,
[
'attr' => [
'placeholder' => 'Enter your Question',
'class' => 'form-control'
],
'required' => true
]
)
->add(
'answer',
TextType::class,
[
'required' => true,
'attr' => [
'class' => 'form-control',
'placeholder' => 'Enter your Answer',
]
]
)
->add(
'save',
SubmitType::class,
[
'attr' => [
'class' => 'btn btn-primary'
]
]
)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Database_Interaction::class,
]);
}
}
还有我的存储库,如果有帮助的话。
<?php
namespace App\Repository;
use App\Entity\Database_Interaction;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
/**
* @method Database_Interaction|null find($id, $lockMode = null, $lockVersion = null)
* @method Database_Interaction|null findOneBy(array $criteria, array $orderBy = null)
* @method Database_Interaction[] findAll()
* @method Database_Interaction[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class Database_InteractionRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Database_Interaction::class);
}
/**
* @param string $id
* @return Database_Interaction|null
*/
public function findById(string $id)
{
return $this->findOneBy(
['id' => $id]
);
}
// /**
// * @return SubmitNew[] Returns an array of SubmitNew objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('s')
->andWhere('s.exampleField = :val')
->setParameter('val', $value)
->orderBy('s.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?SubmitNew
{
return $this->createQueryBuilder('s')
->andWhere('s.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}
如果有人可以帮助我,我不知道在哪里找...
谢谢
答案 0 :(得分:5)
仔细查看这一行:
$entries = $em->getRepository(Database_InteractionType::class)->find($id);
这是指表单类型类(App\Form\Database_InteractionType
)而不是持久化的实体类,它位于App\Entity
名称空间(它正在查找的名称空间)中,这就是产生错误的原因。表单没有存储库,相关实体则有。
试试这个:
$entries = $em->getRepository(Database_Interaction::class)->find($id);