2012-03-02 4 views
0

私は2つのエンティティを持っています。一つは、識別器などで構成されるメインのスーパークラスとして、もう一つはスーパークラスを継承しています... アクションと呼ばれる1つのテーブル。Doctrine 2 Discriminator継承マッピングはfindByメソッドが動作しないことにつながります

私discrimatorエンティティ:

namespace Entities\Members; 

/** 
* @Entity 
* @Table(name="actions") 
* @MappedSuperClass 
* @InheritanceType("JOINED") 
* @DiscriminatorColumn(name="action_type", type="string") 
* @DiscriminatorMap({"comments" = "Comments", "blog" = "Blog"}) 
* @HasLifecycleCallbacksIndex 
*/ 
class Action { 

    /** 
    * @Id 
    * @Column(name="id", type="integer") 
    * @GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** @Column(type="string", length=300, nullable=true) */ 
    public $name; 

    /** @Column(name="action_date", type="datetime", columnDefinition="datetime", nullable=false) */ 
    protected $action_date; 

    /** @PrePersist */ 
    public function updated() { 
     $this->action_date = new \DateTime("now"); 
    } 

} 

これは私が上記discrimatorエンティティを拡張するために使用私のエンティティのいずれかになります。

namespace Entities\Members; 

/** 
* @Entity 
* @Table(name="comments") 
* @HasLifecycleCallbacks 
*/ 
class Comments extends Action { 

    /** 
    * @Id @Column(name="id", type="bigint",length=15) 
    * @GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** @Column(name="blog_id", type="integer", nullable=true) */ 
    protected $blog_id; 

    /** @Column(name="comment", type="string", length=255, nullable=true) */ 
    protected $comment; 

    /** @Column(name="comment_date", type="datetime", columnDefinition="datetime", nullable=true) */ 
    protected $comment_date; 

    /** 
    * @ManyToOne(targetEntity="Members",inversedBy="comments", cascade={"persist"}) 
    * @JoinColumn(name="userid", referencedColumnName="id") 
    */ 
    protected $author; 


    public function __construct() { 
     $this->comment_date = $this->comment_date = new \DateTime("now"); 
    } 

} 

私はコメントエンティティを永続化するとき、これは例えば、うまく動作します

$entity = new Entities\Comments; 
$entity->comment = "my new comment"; 
$this->em->persist($entity); 
$this->em->flush(); 

私が持続する場合、それがアクションテーブルに成功したアクションが追加されます...

しかし、私は= ,,もはやこれらのメソッドからのすべての結果の戻り値を任意のfindBy、findByOneメソッドを使用することはできませんnull コメントクラスを編集して 'アクションからの延長'を削除すると、doctrine findby、findOneByメソッドが動作を開始します しかし、それはメインアクションスーパークラスを拡張しないので、tmyディスクリミネータテーブルに追加されません...

私はアクションを拡張し、doctrineメソッドをsuにする必要がありますchを見つける、findOneBy、等も...

どのような提案?

答えて

0

クエリコードを投稿してください。

protected function testJoinedQuery() 
{ 
    $em = $this->getEntityManager(); 
    $repo = $em->getRepository('Entity\Comments'); 

    $entity = $repo->findOneBy(array('id' => 2)); 

    echo get_class($entity) . ' ' . $entity->getComment() . "\n"; 

} 

がうまく動作するように見えた:これはすべての仕事をどのように私は、私は、あなたのエンティティをコピーして、クエリをした見ることに興味がありました。アクションレポを使用しようとしましたか?

あなたはおそらく、この必要はありません。

  • @MappedSuperclass

しかし、何かを傷つけるしていないようです有します。

+0

いいえ.. get_class($ entity)を実行すると、このコードが実際のクラス名を出力します。BaseController?しかし、私は '拡張アクティビティ'を削除する場合は、エンティティ/コメントを出力するので、アクションを拡張しないとうまくいきます。 –

+0

あなたの質問コードを投稿してください。 – Cerad

+0

あなたと同じ:$ repo = $ this - > _ em-> getRepository( 'Entities \ Comments'); $ entity = $ repo-> findOneBy(array( 'name' => 'bars')); // $ entity = $ repo-> findOneById(233); echo get_class($ entity)。 ''。 $ entity-> id。 "\ n";あなたが上に見ることができるように私は両方を試みた... findOneByIdとfindOneBy(配列)私はそのIDがそれを拡張しているすべてのエンティティと衝突していると思う....(アクションidフィールド) –

関連する問題