dousuo2812 2015-03-12 21:43
浏览 56
已采纳

Symfony 1.4 Propel:通过外键访问表数据

I need data from two MySQL tables - I wrote a Propel query to retrieve data from my 'search' table. Now, I am trying to retrieve data from my 'item_view' table using ->getItemView()->getPosition() within the view. The search_id (search table primary key) is a foreign key in the item_view table but the item_view_id (item view primary key) is not a foreign key in the search table. Any suggestions for how to retrieve this data? Code below.

View:

<?php foreach ($queries as $query) : ?>
<tr>
    <td><?php echo $query->getTs(); ?></td>
    <td><?php echo $query->getUser() ? $query->getUser()->getEmail() : ""; ?></td>
    <td><?php echo $query->getTerm(); ?></td>
    <td><?php echo $query->getResultCount(); ?></td>
    <td><?php echo **$query->getItemViews()->getItem()->getPartName()**; ?></td>
    <td><?php echo **$query->getItemViews()->getPosition()**; ?></td>
</tr>
<?php endforeach; ?>

Controller: (note: I have no problem retrieving user table data because its foreign key is in the search table.

$c = new Criteria();
$c->addDescendingOrderByColumn(SearchPeer::TS);
$this->queries = SearchPeer::doSelect($c);

Is this a case where you cannot retrieve data from a table unless its primary key is the foreign key of the table for which the propel query is formed?

Any solutions are appreciated!

  • 写回答

1条回答 默认 最新

  • dps123456789 2015-03-13 18:44
    关注

    I finally found the solution to my problem, which had less to do with Propel and Symfony and everything to do with my understanding of relational databases.

    There may be multiple item_view rows for a single search row. The ->getItemView() I was trying to originally use should be ->getItemViews() because there is a one to many relationship. It also needs to be nested as its own foreach loop to retrieve values. Correct code below:

    View:

    <?php foreach ($queries as $query) : ?>
    <tr>
        <td><?php echo $query->getTs(); ?></td>
        <td><?php echo $query->getUser() ? $query->getUser()->getEmail() : ""; ?></td>
        <td><?php echo $query->getTerm(); ?></td>
        <td><?php echo $query->getResultCount(); ?></td>
        <?php foreach ($query->getItemViews() as $iav) : ?>
            <td><?php echo $iav->getItem()->getPartName(); ?></td>
            <td><?php echo $iav->getPosition(); ?></td>
        <?php endforeach; ?>
    </tr>
    <?php endforeach; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源