<?php
namespace Jo\Model;
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="resource_type", type="string")
* @DiscriminatorMap({"article" = "\Jo\Model\Article\ArticleVote", "comment" = "\Jo\Model\Article\CommentVote"})
*/
class Vote
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ManyToOne(targetEntity="\Jo\Model\User\User")
*/
protected $user;
/**
* @Column(type="integer")
*/
protected $weight;
public function setWeight($weight)
{
$this->weight = $weight;
return $this;
}
public function getWeight()
{
return $this->weight;
}
}
<?php
namespace Jo\Model\Article;
use Jo\Model;
/**
* @Entity
*/
class CommentVote extends Model\Vote
{
/**
* @ManyToOne(targetEntity="Comment")
*/
protected $comment;
public function setComment(Comment $comment)
{
$this->comment = $comment;
return $this;
}
public function getComment()
{
return $this->comment;
}
}
生成下面的表模式:
CREATE TABLE Vote (
id INT AUTO_INCREMENT NOT NULL,
user_id INT DEFAULT NULL,
article_id INT DEFAULT NULL,
comment_id INT DEFAULT NULL,
weight INT NOT NULL,
resource_type VARCHAR(255) NOT NULL,
INDEX IDX_FA222A5AA76ED395 (user_id),
INDEX IDX_FA222A5A62922701 (article_id),
INDEX IDX_FA222A5AF8697D13 (comment_id),
PRIMARY KEY(id)
) ENGINE = InnoDB;
这看起来是正确的。
然而,当我这样做:
$commentVote = new CommentVote();
$commentVote->setComment($comment); // $comment instance of Comment
$commentVote->setWeight(1);
$em->persist($commentVote);
$em->flush();
我得到以下错误:
Message: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'resource_type' cannot be null
我必须手动设置作为鉴别的RESOURCE_TYPE财产?如果我在两个不同的类上使用Single Table Inheritance,我不会手动指出这一点。
我做错了什么,我找不到有关这种实现的任何有价值的信息。
thnak you。
这对我不起作用。当我这样做的时候,Doctrine告诉我“resource_type”字段已经定义了两次(一次作为descriminator列,一次作为常规列)。 – 2012-08-10 22:38:43