开发者

doctrine 2 - persisting an entity with composite key

A Doctrine 2 Entity with a composite key:

/**
 * @Entity 
 */
class Test
{
    开发者_StackOverflow社区/**
     * @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;
    }

}

Saving the Entity

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

DB Table:

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

Expected result: a row is added to the db table with two id fields, both with a value of 1.

Actual result: No row is added to the db table. No exception is thrown.

Question: What is going on?


You can use a try catch block to see what happens

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

Other assumption, in my opinion, your entity table name and DB table name may not match each other. I think it's not a bad idea to give a try

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜