2011-09-27 78 views
2

继承问题我编辑了一些错误和细节......与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; 
} 

学说:架构:更新--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()发生相同...我意识到,产品的ID是当我使用flush()时自动生成。所以当我使用persist时不会生成...

回答

4

在继承类中不能有两个主键,仅仅因为它可以让您保留基类对象类。你可以找到一个例子here它工作正常。除了它更复杂的使用查询巫婆应该过滤特定的实例,但一切皆有可能