Symfony2、Doctrine2 的继承问题

发布于 2024-12-06 11:12:08 字数 1819 浏览 0 评论 0原文

我编辑了一些错误和细节...

好吧,我一直在尝试创建一个以产品为父级、以电影和书籍为子级的继承。网上查了一下官方文档并没有解决我的问题,因为例子很差而且不完整。 (http://www.doctrine-project.org/docs/orm/2.0/en/reference/inheritance-mapping.html#class-table-inheritance)。

我不确定我是否做对了,现在我只是不知道如何生成、操作和持久化继承的对象。

父类

<?php
namespace Paf\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table()
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({ "film" = "FilmE2" , "book" = "BookE2" })
*/

class ProductEjemplo2 
{

/**
 * @var integer $id
 * @ORM\Id
 * @ORM\Column(name="id", type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;
//More fields, Name, Description ...
}

子类

<?php 
//Paf\MyBundle\Entity\FilmE2
namespace Paf\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table()
* @ORM\Entity
*/

class FilmE2 extends ProductEjemplo2
{
/**
  * @var integer $id
  * @ORM\Id
  * @ORM\Column(name="id", type="integer")
  */
 protected $id;
}

学说:schema:update --force 生成2个表: ProductE...(id 主键,光盘不知道如何工作,其余字段) FilmE2(id 主键,其余字段)

public function create2Action()  
{
$product1 = new ProductEjemplo2();
$product1->setName('New Film 1'); //and more fields
//here $product1 ID is null.(autogenerated but yet without value)
$em->persist($product1); //error, non-object....
$em->flush();

$film = new FilmE2();
$film->setName('New Film 1'); //and more fields
$film->setDirector('dir1');
$film->setId(1);
$em->persist($film); //error, non-object....
$em->flush();
//In both cases happens the same.

这不起作用,这很明显,因为说错误“非对象”无法持久...但是如果我尝试使用新的 Filme2()发生同样的事情...我意识到当我使用flush()时,产品的ID是自动生成的。所以当我使用 persist 时不会生成...

I edited some mistakes and details...

Well, I have been trying to create a inheritance with Product as parent and Film and Book as childs. Checking online and the official documentation didn't solved my problem because examples are poor and incomplete. (http://www.doctrine-project.org/docs/orm/2.0/en/reference/inheritance-mapping.html#class-table-inheritance).

I'm not sure if I did it right and now I just don't know how to generate, manipulate and persist inherited objects.

Parent class

<?php
namespace Paf\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table()
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({ "film" = "FilmE2" , "book" = "BookE2" })
*/

class ProductEjemplo2 
{

/**
 * @var integer $id
 * @ORM\Id
 * @ORM\Column(name="id", type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;
//More fields, Name, Description ...
}

Child Class

<?php 
//Paf\MyBundle\Entity\FilmE2
namespace Paf\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table()
* @ORM\Entity
*/

class FilmE2 extends ProductEjemplo2
{
/**
  * @var integer $id
  * @ORM\Id
  * @ORM\Column(name="id", type="integer")
  */
 protected $id;
}

doctrine:schema:update --force generates 2 tables:
ProductE...(id primary key, disc dunno how works, rest of fields)
FilmE2(id primary key, rest of fields)

public function create2Action()  
{
$product1 = new ProductEjemplo2();
$product1->setName('New Film 1'); //and more fields
//here $product1 ID is null.(autogenerated but yet without value)
$em->persist($product1); //error, non-object....
$em->flush();

$film = new FilmE2();
$film->setName('New Film 1'); //and more fields
$film->setDirector('dir1');
$film->setId(1);
$em->persist($film); //error, non-object....
$em->flush();
//In both cases happens the same.

This doesn't work, it's quite obvious because says error "non-object" cant be persisted... but if I try with a new Filme2() happens the same... I realized that the ID of product is autogenerated when I use flush(). So isn't generated when I use persist...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一直都在从未离去 2024-12-13 11:12:08

在继承类中不能有两个主键,因为它允许您保留基对象类。您可以在此处找到示例,它工作正常。除了使用查询更复杂之外,应该过滤特定实例,但一切皆有可能

You cannot have two primary keys in inherited class, simply because it lets you to persist base object class. You can find an example here it works fine. Except that its more complicated to use queries witch should filter specific instances but everything is possible

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文