2012-12-29 26 views
2

我正在使用傳輸,並且在文章和視頻之間具有ManyToMany關係。我需要做的就是在添加到文章中時爲每個視頻添加一個時間戳。即我有兩個表:將一個屬性添加到多個傳入ORM(Coldfusion)中加入

  • 文章(ID,標題,正文)
  • 視頻(ID,URL)

然後我有一個鏈接表:

  • article_videos( articleID,videoID)

我需要添加一個額外的列timeStamp到article_videos:

  • article_videos(條款ArticleID,視頻ID,時間戳)

我的問題是,當我試圖創建鏈接表的額外的屬性這是行不通的。

我轉移ORM配置:

<package name="article"> 
    <object name="Article" table="article"> 
     <id name="ID" type="numeric"/> 
     <property name="Title" type="string" column="title"/> 
     <property name="Body" type="string" column="body"/> 

     <manytomany name="Videos" table="article_videos"> 
      <link to="article.Atricle" column="articleID"/> 
      <link to="assets.Video" column="videoID"/> 
      <collection type="array"> 
       <order property="OrderIndex" order="asc"/> 
      </collection> 
      <property name="TimeStamp" type="timestamp" column="timeStamp"/> 
     </manytomany> 
    </object> 
</package> 

<package name="assets"> 
    <object name="Video" table="video"> 
     <id name="ID" type="numeric"/> 
     <property name="url" type="string" column="url"/> 
    </object> 
</package> 

的問題是多對多內的新特性是不允許的,它拋出一個錯誤,指出該傳輸配置的格式不正確。

我應該在哪裏以及如何添加時間戳記,因爲該視頻可能在多篇文章中使用,因此時間戳需要用於該文章中的視頻?

在此先感謝。

回答

3

您可能需要做的是爲您的article_videos連接創建一個新對象。

由於Transfer ORM透明地處理多對多連接,所以如果您想要在連接上添加和訪問其他屬性,您將不需要直接與連接表交互,您需要創建一個新對象。有幾種方法可以實現這一點。

如果您仍想透明地處理多對多關係,並且時間戳會自動填充到數據庫中,您可以保持關係不變,並添加一個代表article_videos的新對象以及將該對象連接到的新關係文章和視頻對象。

所以,你會添加表示article_videos一個新的對象,我還可以添加一個代理鍵到數據庫,或者您可能需要使用複合ID:

<object name="ArticleVideo" table="article_videos"> 
    <id name="ID" type="numeric"/> 
    <property name="TimeStamp" type="timestamp" column="timeStamp"/> 
</object> 

那麼你會更新Article對象引用這個新的對象:

<object name="Article" table="article"> 
    <id name="ID" type="numeric"/> 
    <property name="Title" type="string" column="title"/> 
    <property name="Body" type="string" column="body"/> 

    <manytomany name="Videos" table="article_videos"> 
    <link to="article.Article" column="articleID"/> 
    <link to="assets.Video" column="videoID"/> 
    <collection type="array"> 
     <order property="OrderIndex" order="asc"/> 
    </collection> 
    </manytomany> 

    <onetomany name="ArticleVideo"> 
    <link to="article.ArticleVideo" column="articleID"/> 
    <collection type="array"> 
     <order property="TimeStamp" order="asc"/> 
    </collection> 
    </onetomany> 
</object> 

而你也將更新Video對象:

<object name="Video" table="video"> 
    <id name="ID" type="numeric"/> 
    <property name="url" type="string" column="url"/> 

    <onetomany name="ArticleVideo"> 
    <link to="article.ArticleVideo" column="videoID"/> 
    <collection type="array"> 
     <order property="TimeStamp" order="asc"/> 
    </collection> 
    </onetomany> 
</object> 

這樣,您就可以使用正常的ArticleVideo對象的關係,但如果你需要訪問附加屬性:

// Creating the join using the many-to-many relationship 
article = transfer.get("article.Article", 1); 
article.addVideo(video); 

然後你也可以訪問的加入,你如何獲得上述信息相關聯關係到加盟可能需要一些工作,所以你可能將不得不在前面是否加入你創建你會想要更多的信息決定:

// Creating the join using the ArticleVideo object 
articleVideo = transfer.new("article.ArticleVideo"); 
articleVideo.addParentArticle(article); 
articleVideo.addParentVideo(video); 
transfer.save(articleVideo); 

// Using a composite ID to access the ArticleVideo 
articleVideo = transfer.get("article.ArticleVideo", { 
    "ArticleID" = 1, 
    "VideoID" = 1 
}); 
WriteOutput("The timestamp is: #articleVideo.getTimeStamp()#"); 

否則,你可以調整所有的關係,但是這將需要渴望在視頻和文章之間使用中間對象。如果它只是一個時間戳,那麼它可能是沒有必要的。