Symfony2、Doctrine2 的继承问题
我编辑了一些错误和细节...
好吧,我一直在尝试创建一个以产品为父级、以电影和书籍为子级的继承。网上查了一下官方文档并没有解决我的问题,因为例子很差而且不完整。 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在继承类中不能有两个主键,因为它允许您保留基对象类。您可以在此处找到示例,它工作正常。除了使用查询更复杂之外,应该过滤特定实例,但一切皆有可能
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