2

私は転送を使用しており、記事とビデオの間に ManyToMany の関係があります。私ができるようにする必要があるのは、記事に追加されたときに各ビデオにタイムスタンプを貼り付けることです。つまり、2 つのテーブルがあります。

  • 記事(ID、タイトル、本文)
  • 動画(ID、URL)

次に、リンクされたテーブルがあります。

  • article_videos(記事ID、ビデオID)

article_videos に追加の列 timeStamp を追加する必要があります。

  • article_videos(記事ID、ビデオ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>

問題は、ManyToMany 内の新しいプロパティが許可されていないことです。転送構成の形式が正しくないというエラーがスローされます。

ビデオが複数の記事で使用される可能性があるため、その記事のそのビデオのタイムスタンプが必要であることを念頭に置いて、タイムスタンプのむき出しをどこにどのように追加すればよいですか?

前もって感謝します。

4

1 に答える 1

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()#");

それ以外の場合は、すべての関係を調整できますが、これにはビデオと記事の間に中間オブジェクトを使用する必要があります. 単なるタイムスタンプである場合、不要な場合があります。

于 2012-12-29T11:49:20.137 に答える