1

reverse何らかの理由で、propel はビュー テーブルのモデルを生成しません。タスクを使用すると、ビュー テーブルの構造も含まれません。そのため、カスタム クエリを使用する以外に選択肢はありません。モデルが存在する場合の方法を知っていること:

<?php 
    $con = Propel::getConnection(BookPeer::DATABASE_NAME);
    $sql = "complicated query here...";
    $stmt = $con->prepare($sql);
    $stmt->execute();

しかし、propel はビュー テーブルのモデルを生成しないため、その方法がわかりません。私はこれを試しましたが、うまくいきません

<?php 
    $con = Propel::getConnection(MyViewTable::DATABASE_NAME);
    $sql = "SELECT * FROM MyViewTable";
    $stmt = $con->prepare($sql);
    $stmt->execute();

私は本当にこの仕事をする必要があります。助けてください :)

4

2 に答える 2

5

もう 1 つの方法は、次のように「readonly」属性と「skipSql」属性を「true」に追加して設定することで、ビューを定義することです。

<table name="BookAuthor" phpName="BookAuthor" readOnly="true" skipSql="true">
  <column type="integer" size="10" name="AuthorID" phpName="AuthorID" />
  <column type="integer" size="10" name="BookID"   phpName="BookID" />
  <!-- Some other columns, etc. -->

  <foreign-key foreignTable="Author">
        <reference local="AuthorID" foreign="ID" /><!-- Assuming you have Author.ID -->
  </foreign-key>
  <foreign-key foreignTable="Book">
        <reference local="BookID" foreign="ID" /><!-- Assuming you have Book.ID -->
  </foreign-key>
</table>

(「propel-gen」コマンドを使用して) クラスを生成すると、次のように、テーブルであるかのように利点が得られます。

// Fetch a BookAuthor row (the view)
$oBookAuthor = BookAuthor::create()->findOne();

// Get the Author (physical table)
$oUser = $oBookAuthor->getAuthor();

// and if you want the "Book" (physical table)
$oBook = $oBookAuthor->getBook();

また、DB への新しい接続も必要ありません

于 2014-06-18T21:33:54.573 に答える
1
$con = Propel::getConnection();

現在のデータベース接続を取得し、好きな SQL クエリを作成できます。

于 2013-07-12T10:19:20.923 に答える