我想首先说我是Silver Stripe开发和PHP / MySQL的新手,所以请善待。
我有一个网站要求我有一个定价页面,在定价页面(duh)和主页上显示公司的定价计划。因此,我们的目标是能够使用Pricing.php
从我的HomePage.ss
页面类型访问数据。
我正在尝试将此作为未来的证据,因此我创建了一个扩展DataObject的ServicePlan.php
并使用FieldList
创建定价记录并将它们与{{1}相关联页面类型通过Pricing
和$has_many
关系。基于我有限的知识,这似乎是实现这一目标的最佳方式。
这是我为完成此任务而编写的代码。
ServicePlan.php
$has_one
Pricing.php
class ServicePlan extends DataObject {
private static $db = array (
'PlanColor' => 'Varchar',
'PlanName' => 'Varchar',
'PlanPrice' => 'Varchar',
'PlanRenewal' => 'Varchar',
);
private static $has_one = array (
'Pricing' => 'Pricing'
);
private static $summary_fields = array (
'PlanName' => 'Name of Plan',
'PlanPrice' => 'Price',
'PlanRenewal' => 'Renewal Period',
);
public function getCMSFields() {
$fields = FieldList::create(
TextField::create('PlanName'),
TextField::create('PlanRenewal'),
TextField::create('PlanPrice'),
TextField::create('PlanColor')
);
return $fields;
}
public function getServicePlans() {
return $this->Pricing();
}
HomePage.php
class Pricing extends Page {
private static $has_many = array (
'ServicePlans' => 'ServicePlan',
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Plans', GridField::create(
'ServicePlans',
'Orenda Force Service Plan Information',
$this->ServicePlans(),
GridFieldConfig_RecordEditor::create()
));
return $fields;
}
}
class Pricing_Controller extends Page_Controller {
}
布局/ HomePage.ss
class HomePage extends Page {
}
class HomePage_Controller extends Page_Controller {
public function ServiceTeasers($count = 2) {
$holder = ServicesHolder::get()->First();
return ($holder) ? ServicesPage::get()->limit($count) : false;
}
public function MembershipPlans() {
$Pricing = Pricing::get();
return $plan;
}
}
我在访问内容时没有问题
布局/ Pricing.ss
...//
<% loop $MembershipPlans %>
<div class="col-1-3 card pricing">
<div class="pricing__header primary $PlanColor">
<p class="pricing__plan-name">$PlanName</p>
<p class="pricing__price">$PlanPrice<sub>$PlanRenewal</sub></p>
</div>
<div class="card__contents">
<h3 class="title-06">Plan details</h3>
<ul>
<li>unlimited training</li>
<li>individual nutrition/meal plans</li>
</ul>
</div>
</div>
$Content
<% end_loop %>
...//
我非常感谢你们所能提供的任何帮助,我一直试图解决这个问题几天,而且我一次又一次地碰壁砖。我做错了什么?
This question是我得到的最接近的,似乎没有解决我的问题。我应该将<section class="body pricing">
<div class="content">
<h2 class="title-02 text-monochrome">Pricing</h2>
<% loop $ServicePlans %>
<div class="col-1-3 card pricing">
<div class="pricing__header $PlanColor">
<p class="pricing__plan-name">$PlanName</p>
<p class="pricing__price">$PlanPrice<sub>$PlanRenewal</sub></p>
</div>
<div class="card__contents">
<h3 class="title-06">Plan details</h3>
<ul>
<li>unlimited training</li>
<li>individual nutrition/meal plans</li>
</ul>
</div>
</div>
<% end_loop %>
</div>
</section>
移到FieldList
页吗?
答案 0 :(得分:0)
对于您的主页,您似乎已经拥有了大部分内容。您只是错过了Price页面所具有的has_many
的循环。由于MembershipPlans()
中的HomePage_Controller
函数会将所有价格页面作为DataList
返回,因此您可以通过循环关系名称来访问相关对象,在本例中为<% loop $ServicePlans %>
。
<强> HomePage.ss 强>
<% loop $MembershipPlans %><!-- loop Price pages -->
<% loop $ServicePlans %><!-- this is your loop for the ServicePlans related to the particular Price page -->
<div class="col-1-3 card pricing">
<div class="pricing__header primary $PlanColor">
<p class="pricing__plan-name">$PlanName</p>
<p class="pricing__price">$PlanPrice<sub>$PlanRenewal</sub></p>
</div>
<div class="card__contents">
<h3 class="title-06">Plan details</h3>
<ul>
<li>unlimited training</li>
<li>individual nutrition/meal plans</li>
</ul>
</div>
</div>
<% end_loop %><!-- end ServicePlans relation loop -->
$Content<!-- Content for particular Price page -->
<% end_loop %><!-- end Price pages loop -->