Question

I have a custom joomla MVC component created by http://component-creator.com with 4 tables:

#__mycomponent_items    27 Fields   
#__mycomponent_bids         12 Fields   
#__mycomponent_journeys     9 Fields    
#__mycomponent_users    8 Fields

I am trying to set the relationships between these tables, but in the absence of documentation and experience I am struggling.

The basic relationships between the tables need to allow USERS to make BIDS to deliver ITEMS.

So I have created fields for items like this:

#__mycomponent_items       
id
created
updated
ordering
state
checked_out
checked_out_time
created_by
deliverydestination
itemtitle
status
requiredby
listprice
deliveredprice
commission
points_reward
accepted_bid
accepted_bidder
accepted_journey

And for bid like this:

#__mycomponent_bids
id
state
created_by
item_id
buyer
bid
created
updated
bid_status
bid_expires
journey
arrival_date

I am working in templates/mytemplate/html/com_mycomponent/item/default.php and trying to add to that view a list of the current bids on that item. To do that I assume I need to add a custom function to /components/com_mycomponent/models/item.php and the function I have created is follows:

function itemBids() {
        // Get a db connection.
        $db = JFactory::getDbo();

        // Create a new query object.
        $query = $db->getQuery(true);

        // Select item record matching the $orderID
        $query
            //->select('*')
            ->select($db->quoteName(array('id', 'created_by', 'created', 'bid', 'bid_status', 'arrival_date')))
            ->from($db->quoteName('#__mycomponent_bids'))
            ->where('item_id = item.id');            
        // Reset the query using our newly populated query object.

        // Load the results as a list of stdClass objects (see later for more options on retrieving data).
        $db->setQuery($query);
        $itemBids = $db->loadObjectList();
        //print_r($itemBids);
}

How do I then access the data in the view /mytemplate/html/com_mycomponent/item/default.php?

I have tried this and it returns nothing:

<ul>
          <?php foreach ($itemBids as $itemBid) :?>
         <?php $arrivalDate = $itemBid->arrival_date; ?>
          <li><strong><?php echo $itemBid->created_by; ?></strong> <small>can deliver for</small> $<?php echo $itemBid->bid;?> <small>
          <?php /*?><abbr class="timeago" title="<?php echo $itemBid->created; ?>"></abbr><?php */?>
          in <strong><abbr class="timeago" title="<?php echo $arrivalDate; ?>"></abbr></strong></small><div class="uk-badge uk-float-right"><?php echo $itemBid->bid_status; ?></div></li>
          <?php endforeach; ?>
        </ul>

No correct solution

OTHER TIPS

You wouldn't be putting it in your template like that. the template just holds the layouts that generate the html. Also you need to have a default layout in your views/myview/tmpl folder.

You don't seem to be returning anything from your itemBids() function. You would want to add return $itemBids; or possibly return $this->itemBids; depending on what you are doing elsewhere.

YOu want to get $this->itemBids in your view.html.php class so that it is then available to your layout. THen instead of referring to $itemBids you can refer to $this->itemBids in your loop in the layout.

Have you been through the Creating an MVC Cmponent tutorial? It would probably help you get a sense of how MVC works in Joomla.

OK , as far as I understand you have a method in yourmodel.php and you are trying to access it from the view I did notice that you are not returning any values in your method

    return $db->loadObjectList();

let me just make it simple by the below code

//com_component/models/yourmodel.php
class componentModelYourmodel extends JModelLegacy
{
function yourMethod()
{
//code
return $value;
}
}

And then in view file

//com_component/views/yourview/tmpl/default.php

//get the model first
$model=$this->getModel();
//call the method
$items=$model->yourMethod();
//print
print_r($items);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top