如何在树枝视图中显示遗产实体数据?

How to display heritage entities data in twig view?

我在 Bootstrap 模态中使用带有内置表单的 Symfony 3.4,并且我创建了一个具有抽象父实体 (USER) 和两个子实体(EMPLOYER 和 SEASONAL,其中 ID字段被注释掉以使用 USER 的字段),它使用 PROTECTED 字段的 3 个实体扩展 USER。

在我的网站上,从我的 twig modalForm 视图,我设法通过我的 SecurityController 在我的不同类型的用户 Employer 或 Seasonal 中注册。

记录完成后,我重定向到 userAccount twig 视图,其 URL 正在接收用户创建发送的数据:MySeason / {role} / {id} / my-account 和 UserAccountController它应该恢复之前发送的数据并显示它们,那是我遇到错误的时候:

使用参数 ["1"] 执行 'SELECT t1.id AS id_2, t1.email AS email_3, t1.password AS password_4, t1.registration AS registration_5, t1.company AS company_6, t1.contact AS contact_7, t1.role AS role_8 FROM employer t1 WHERE t0.id = ?' 时发生异常: SQLSTATE[42S22]:找不到列:1054 字段 't0.id' 在 where 子句中未知

我认为在错误中,symfony 混淆了子实体的 t1.id 和父实体的 t0.id。

如果有人有解决方案可以提供给我! ;-)

我的父实体

...
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="P6\GeneralBundle\Repository\UserRepository")
*/
abstract class User
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255)
*/
protected $email;

/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
protected $password;
...

我的女儿实体

...
/**
* Employer
*
* @ORM\Table(name="employer")
* @ORM\Entity(repositoryClass="P6\GeneralBundle\Repository\EmployerRepository")
*/
class Employer extends User
{
const ROLE_USER = 'EMPLOYER';

// /**
// * @var int
// *
// * @ORM\Column(name="id", type="integer")
// * @ORM\Id
// * @ORM\GeneratedValue(strategy="AUTO")
// */
// protected $id;

/**
* @var string
*
* @ORM\Column(name="company", type="string", length=255)
*/
protected $company;

/**
* @var string
*
* @ORM\Column(name="contact", type="string", length=255)
*/
protected $contact;
...

我的安全控制器

<?php

namespace P6\GeneralBundle\Controller;

use P6\GeneralBundle\Entity\Seasonal;
use P6\GeneralBundle\Form\SeasonalType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use P6\GeneralBundle\Entity\Employer;
use P6\GeneralBundle\Form\EmployerType;

class SecurityController extends Controller
{

public function userRegistrationAction (Request $request)
 {
 // Employer Form

$employer = new Employer();

 if ($formEmployer = $this->createForm(EmployerType::class, $employer, array('action' => $this->generateUrl('registerUser')
 ))) {

$formEmployer->handleRequest($request);

if ($formEmployer->isSubmitted() && $formEmployer->isValid()) {
/** @var Employer $employer */
$employer = $formEmployer->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($employer);
$em->flush();

$this->addFlash('registration', 'Votre profil a bien été enregistré !');

return $this->redirectToRoute('user_account', [
'role' => $employer->getRole(),
'id' => $employer->getId(),
]);
}
}

// Seasonal Form

$seasonal = new Seasonal();

 if ($formSeasonal = $this->createForm(SeasonalType::class, $seasonal, array('action' => $this->generateUrl('registerUser')
 ))) {

$formSeasonal->handleRequest($request);

if ($formSeasonal->isSubmitted() && $formSeasonal->isValid()) {
/** @var Seasonal $seasonal */
$seasonal = $formSeasonal->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($seasonal);
$em->flush();

$this->addFlash('registration', 'Votre profil a bien été enregistré !');

return $this->redirectToRoute('user_account', [
'role' => $seasonal->getRole(),
'id' => $seasonal->getId(),
]);
}
}

 // Renvoi de vue si les formulaires ne sont pas valides

return $this->render('@General/Default/modalForm.html.twig', [
'formEmployer' => $formEmployer->createView(),
'formSeasonal' => $formSeasonal->createView(),
]);
}
}

我的用户帐户控制器

<?php

namespace P6\GeneralBundle\Controller;

use P6\GeneralBundle\Entity\Employer;
use P6\GeneralBundle\Entity\Seasonal;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class UserAccountController extends Controller
{
public function userAccountAction(Employer $employer, Seasonal $seasonal)
 {
$repository = $this->getDoctrine()->getRepository('GeneralBundle:Employer');
$employer = $repository->findBy(['employer' => $employer->getId()]);

//        if ($repository = $this->getDoctrine()->getRepository('GeneralBundle:Seasonal')) {
//            $seasonal = $repository->findBy(['seasonal' => $seasonal->getId()]);
//        }

return $this->render('@General/Default/userAccount.html.twig', [
'employer' => $employer,
//'seasonal' => $seasonal,
]);

}
}

我已经尝试了 3 种方法来扩展 class(映射的超级 class、单个 table 和 class table),但这并没有解决我的问题。 我希望能够恢复保存在数据库中的数据并显示在树枝视图中。

通过这个link(Symfony2-Doctrine2 inheritance persist not working properly)我找到了解决办法,我终于可以恢复用户注册时发送的数据到下一个视图,用户的帐户!

其实老妈的参数里还得加上这段代码class:

* @ORM \ InheritanceType ("JOINED")
* @ORM \ DiscriminatorColumn (name = "discr", type = "string")
* @ORM \ DiscriminatorMap ({"employer" = "Employer", "seasonal" = "Seasonal"})

但也通过替换修改我的女儿实体的 getters Id:

public function getId (){return $ this-> id;}

作者:

public function getId (){return parent :: getId ();}

最后,我在我的 UserAccountController 中替换了:

$ repository = $ this-> getDoctrine () -> getRepository ('GeneralBundle: Employer');
$ employer = $ repository-> findBy (['employer' => $ employer-> getId ()]);

作者:

$ repository = $ this-> getDoctrine () -> getRepository ('GeneralBundle: Employer');
$ employer = $ repository-> findOneBy (['id' => $ employer-> getId ()]);

希望这个解决方案不会阻止我剩下的时间;)