-2

I have a custom joomla MVC component. The component has a table of items, and a table of bids to deliver those items.

I have figured out how to display a list the relevant 'bids' on the view for an 'item' by adding this to the item model:

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

This to the views/item/view.html.php:

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

And e.g. this to the views/item/tmpl/default.php:

var_dump($this->items_by_id);

That all works fine.

Here's the actual question:

Now i need to show a COUNT of the bids on the LIST view (items - with an s) within each row of the foreach.

What is the BEST way of achieving this? I have tried adding the same to the items model and views/items/view.html.php, then I attempted something like this in the list:

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

But that just returns 0 - not the count of bids on each item in the list. Grateful for help from the MVC experts out there.

tereško
  • 58,060
  • 25
  • 98
  • 150
larpo
  • 1,073
  • 1
  • 10
  • 18
  • 1
    http://us3.php.net/count Since $item->bidsOnItem is an object ( you're reurning an objectlist) I believe you actually ned to give it something countable. However I am really confused about why you wouldn't return the count from the query if that's what you want. Why not just make a getBidsCount method? Also JDatabaseQuery has a getNumRows() method which returns he number of rows returned by the previous query. – Elin Apr 09 '14 at 03:16
  • I would agree with Elin, did you try it with a row count? Or do you need the bids objects for some other reason? - or, did you try sizeof($item->bidsOnItem)? – elk Apr 09 '14 at 14:36
  • thanks - ok no, on the items list view I just want the count, so I take from what you are saying I need to rewrite the query to be a count using something like: select COUNT(*), bids GROUP BY item? could you possibly propose the full entry I need in models/items.php? - also Elin, I sent you an email through your website. Grateful for your view when you get a minute. – larpo Apr 09 '14 at 18:17

2 Answers2

0

You've gone and got yourself mixed up there. For a start to assign variables to a template, you need to do the following:

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

Also, your query returns a list of items rather than a multi-dimensional array of items. To get the count of bids by item, you need to run a separate query for each item

Eg.

Model

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

    function getBidsByItemId($itemId) {
        // query goes here
        // return results
    }
}

View

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

And then use your for loop and instead of print count($blah); just use print $item->bids.

Justin Mitchell
  • 339
  • 1
  • 5
  • 1
    This would be really inefficient. You don't want to run a separate query for each item. First of all, if nothing else, run one query that does `select COUNT(*), bids GROUP BY item.` From my understanding of the question though, he really does not want a list of items he wants the list of bids for a given item. – Elin Apr 09 '14 at 12:02
  • yes - exactly. thanks. I have the list of items. I want to show bids per item on the items list. – larpo Apr 09 '14 at 12:35
0

I have rephrased this question (hopefully in a more useful way) here: Add a count of related data to each item in a joomla MVC list view

Community
  • 1
  • 1
larpo
  • 1,073
  • 1
  • 10
  • 18