Propel custom sql for view tables

蹲街弑〆低调 提交于 2020-03-09 05:32:09

问题


For some reason propel is not generating model for view tables, it doesn't even include the structure of the view table if you use the reverse task. So I have no option but to use a custom query. That I know how to do if the model exists:

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

but since propel does not generate a model for my view table, i don't know how to do that. I've tried this but it doesn't work

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

I really need to have this work. Please help :)


回答1:


$con = Propel::getConnection();

You will get your current database connection and you can make any sql query you like,




回答2:


Another alternative is to define your view by adding and setting "readonly" and "skipSql" attributes to "true" like this:

<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>

Once you've generated you classes (via "propel-gen" command), you will have the benefits as though it were a table, like this:

// 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();

And you don't even need a new connection to the DB



来源:https://stackoverflow.com/questions/17566863/propel-custom-sql-for-view-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!