【问题标题】:Propel custom sql for view tables推动视图表的自定义 sql
【发布时间】:2013-07-10 09:19:39
【问题描述】:

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

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

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

【问题讨论】:

    标签: mysql view propel


    【解决方案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();
    

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

    【讨论】:

      【解决方案2】:
      $con = Propel::getConnection();
      

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

      【讨论】:

      • 我可以使用查询结果来填充我的对象吗?
      • while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //获取你的数据 }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      相关资源
      最近更新 更多