php - Help me to understand Single Class Inheritance in Doctrine 2 -


<?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;     } } 

and

<?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;     } } 

it generates following table schema :

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; 

which looks correct.

however, when :

$commentvote = new commentvote(); $commentvote->setcomment($comment); // $comment instance of comment $commentvote->setweight(1); $em->persist($commentvote); $em->flush(); 

i following error :

message: sqlstate[23000]: integrity constraint violation: 1048 column 'resource_type' cannot null  

do have manually set resource_type property used discriminator? don't point manually if use single table inheritance on 2 different class.

do wrong, couldn't find valuable information kind of implementation.

thnak you.

commentvote extending vote. vote resource_type. have set commentvote, , can not null:

resource_type varchar(255) not null 

you have want resource_type be. can not guess that.

you can set nullable in vote:

/**  * @column(type="string", length=255", nullable=true)  */ private $resource_type; 

or can set default value resource_type. if so, can choose set default value in __construct of commentvote, or can set value in @preupdate method.


Comments