2011-04-17 58 views
0

学说2实体的实体具有复合键:学说2 - 持续复合键

/** 
* @Entity 
*/ 
class Test 
{ 
    /** 
    * @Id 
    * @Column (type="integer", length=11, name="id") 
    * 
    */ 
    protected $id = null; 

    /** 
    * @Id 
    * @Column (type="integer", length=11, name="idtwo") 
    * 
    */ 
    protected $idtwo = null; 

    public function setIdTwo($id) 
    { 
     $this->idtwo = $id; 
    } 

    public function setId($id) 
    { 
     $this->id = $id; 
    } 

} 

保存实体

$test = new Test(); 
$test->setId(1); 
$test->setIdTwo(1); 
$em->persist($test); 

DB表:

CREATE TABLE `Bella_Test` (
    `id` int(11) NOT NULL, 
    `idtwo` int(11) NOT NULL, 
    PRIMARY KEY (`id`,`idtwo`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

预期结果:将一行添加到带有两个ID字段的数据库表中,其值为1.

实际结果:没有行被添加到数据库表中。没有例外被抛出。

问题:发生了什么事?

+2

你叫'$ EM->的flush()' – rojoca 2011-04-18 02:49:09

+0

使用复合材料的PK,确保他们的工作在理论上很好,但你有没有尝试过删除从来不喜欢或更新一个?该应用程序需要知道这两个值,而不是一个PK,应用程序只需要知道一个ID。 – 2011-12-01 01:00:10

回答

0

您可以使用try catch块来看看会发生什么

try{ 
    $em->flush(); //don't forget flush after persisting an object 
} 
catch(Exception $e){ 
    echo 'Flush Operation Failed: '.$e->getMessage(); 
} 

其他假设,在我看来,你的实体表名和数据库表名可能相互不匹配。我认为这不是一个坏主意,让一个尝试

/** 
* @Entity 
* @Table(name="Bella_Test") 
*/ 
. 
. 
.