Propel custom sql for view tables

前端 未结 2 1146
情话喂你
情话喂你 2020-12-06 23:45

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

相关标签:
2条回答
  • 2020-12-07 00:38
    $con = Propel::getConnection();
    

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

    0 讨论(0)
  • 2020-12-07 00:38

    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

    0 讨论(0)
提交回复
热议问题