0

我有一个由http://component-creator.com创建的自定义 joomla MVC 组件,其中包含 4 个表:

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

我正在尝试设置这些表之间的关系,但是在没有文档和经验的情况下,我正在苦苦挣扎。

表之间的基本关系需要允许用户进行投标以交付项目。

所以我为这样的项目创建了字段:

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

对于这样的出价:

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

我正在模板/mytemplate/html/com_mycomponent/item/default.php 中工作,并尝试向该视图添加该项目的当前出价列表。为此,我假设我需要向 /components/com_mycomponent/models/item.php 添加一个自定义函数,并且我创建的函数如下:

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

然后如何访问视图/mytemplate/html/com_mycomponent/item/default.php 中的数据?

我已经尝试过了,它什么也没返回:

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

2 回答 2

0

你不会像那样把它放在你的模板中。该模板仅包含生成 html 的布局。您还需要在您的views/myview/tmpl 文件夹中有一个默认布局。

你似乎没有从你的itemBids()函数中返回任何东西。您可能想要添加return $itemBids;或可能return $this->itemBids;取决于您在其他地方所做的事情。

您想在 view.html.php 类中获取 $this->itemBids 以便它可以用于您的布局。$itemBids然后,您可以$this->itemBids在布局中的循环中引用而不是引用。

您是否完成了创建 MVC 组件教程?它可能会帮助您了解 MVC 在 Joomla 中的工作原理。

于 2014-03-02T06:02:35.567 回答
0

好的,据我了解,您在 yourmodel.php 中有一个方法,并且您正试图从视图中访问它,我确实注意到您没有在方法中返回任何值

    return $db->loadObjectList();

让我通过下面的代码让它变得简单

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

然后在视图文件中

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

//get the model first
$model=$this->getModel();
//call the method
$items=$model->yourMethod();
//print
print_r($items);
于 2014-03-03T00:33:11.997 回答