推动视图表的自定义sql

use*_*000 1 mysql propel view

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

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

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

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

我真的需要这项工作.请帮忙 :)

Qin*_*iso 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>
Run Code Online (Sandbox Code Playgroud)

一旦你生成了类(通过"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();
Run Code Online (Sandbox Code Playgroud)

而且您甚至不需要与DB的新连接