gpt4 book ai didi

php - 如何向 MVC Joomla 组件添加函数以访问不同表中的数据?

转载 作者:行者123 更新时间:2023-11-30 00:36:32 25 4
gpt4 key购买 nike

我有一个由 http://component-creator.com 创建的自定义 joomla MVC 组件有 4 张 table :

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

我正在尝试设置这些表之间的关系,但由于缺乏文档和经验,我很挣扎。

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

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

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

我正在 templates/mytemplate/html/com_mycomponent/item/default.php 中工作,并尝试向该 View 添加该项目的当前出价列表。为此,我假设我需要向/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);
}

如何访问 View /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>

最佳答案

您不会像那样将其放入模板中。模板只包含生成 html 的布局。此外,您还需要在views/myview/tmpl 文件夹中有一个默认布局。

您的 itemBids() 函数似乎没有返回任何内容。您可能需要添加 return $itemBids; 或可能 return $this->itemBids;,具体取决于您在其他地方执行的操作。

您希望在 view.html.php 类中获取 $this->itemBids ,以便您的布局可以使用它。那么,您可以在布局的循环中引用 $this->itemBids,而不是引用 $itemBids

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

关于php - 如何向 MVC Joomla 组件添加函数以访问不同表中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22123947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com