1

由于某种原因,propel 没有为视图表生成模型,如果您使用该reverse任务,它甚至不包括视图表的结构。所以我别无选择,只能使用自定义查询。如果模型存在,我知道该怎么做:

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

但由于推进不会为我的视图表生成模型,我不知道该怎么做。我试过这个,但它不起作用

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

我真的需要这份工作。请帮忙 :)

4

2 回答 2

5

另一种选择是通过添加“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();

而且您甚至不需要与数据库的新连接

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

您将获得当前的数据库连接,并且可以进行任何您喜欢的 sql 查询,

于 2013-07-12T10:19:20.923 回答