如何显示一行';在Joomla MVC列表视图中的关系数据


How to show a row's relational data in a Joomla MVC list view?

我有一个自定义的joomla MVC组件。该组件具有项目表,以及交付这些物品的投标表。

我已经找到了如何通过将其添加到项目模型中来在视图上显示"项目"的相关"出价"列表:

 public function getBidsById($id) {
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query
            ->select('*')
            ->from('#__entrusters_bids ')
            ->where('item_id = ' . $id);
    $db->setQuery($query);
    $db->query();
    return $db->loadObjectList();
}

这到了views/item/view.html php:

    $model = $this->getModel('Item', 'EntrustersModel');
    $itemID = jRequest::getVar('id');
    $bidsOnItem = $model->getBidsById($itemID);
    $this->bidsOnItem = $bidsOnItem;

例如,将其添加到视图/item/tmpl/default.php:

var_dump($this->items_by_id);

这一切都很好。

以下是实际问题:

现在我需要在foreach的每一行的LIST视图中显示投标的计数(项目-带s)

实现这一目标的最佳方式是什么?我已经尝试将其添加到items模型和views/items/view.html.php中,然后我尝试在列表中添加类似的内容:

<ul> 
      <?php foreach ($this->items as $item) : ?>
 <li>Item stuff about the item. This item has <?php echo count($item->bidsOnItem); ?> bids</li>
    <?php endforeach; ?>
</ul>

但这只会返回0,而不是列表中每个项目的出价数。感谢MVC专家的帮助。

你去了,把自己搞混了。要开始为模板分配变量,您需要执行以下操作:

$bar = 'bar';
$this->assignRef('foo', $bar);

此外,查询返回项目列表,而不是多维项目数组。要按项目获取投标计数,您需要对每个项目运行单独的查询

例如。

型号

class SomethingModelTest extends JModelLegacy {
    function getItemList() {
        // query goes here
        // return results
    }
    function getBidsByItemId($itemId) {
        // query goes here
        // return results
    }
}

查看

class SomethingViewTest extends LegacyView {
    public function display() {
        $items = $this->get('ItemList');
        foreach ($items as &$item) {
            $item->bids = $this->getModel()->getBidsByItemId($item->itemId);
        }
        $this->assignRef('items', $items);
        parent::display();
    }
}

然后使用for循环,而不使用print count($blah);,只使用print $item->bids

我在这里重新表述了这个问题(希望以更有用的方式):在joomla MVC列表视图中的每个项目中添加一个相关数据计数