原则 2 - 使用组合键持久化实体
具有组合键的 Doctrine 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;
预期结果:将一行添加到 db 表,其中有两个 id 字段,其值均为 1。
实际结果:没有行添加到 db 表。没有抛出异常。
问题:这是怎么回事?
A Doctrine 2 Entity with a composite key:
/**
* @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;
}
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 try catch 块来查看会发生什么
其他假设,在我看来,您的实体表名称和数据库表名称可能不匹配。我认为尝试一下并不是一个坏主意
You can use a try catch block to see what happens
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