Silverstripe 从单独的页面类型访问 has_many 数据

Silverstripe accessing has_many data from a separate page type

首先我想说我是 Silver Stripe 开发的新手,一般来说 PHP/MySQL,所以请多多关照。

我有一个网站要求我有一个定价页面,在定价页面 (duh) 和主页上显示公司的定价计划。因此,目标是能够使用 HomePage.ss 从我的 Pricing.php 页面类型访问数据。

我试图尽可能将其作为未来的证据,因此我创建了一个 ServicePlan.php 来扩展 DataObject 并使用 FieldList 创建定价记录并将它们与 Pricing 页面类型通过 $has_many$has_one 关系。根据我有限的知识,这似乎是完成此任务的最佳方法。

这是我为此编写的代码。

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

Pricing.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.php

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

Layout/HomePage.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 %>

...//

我在访问内容时没有问题

Layout/Pricing.ss

<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 移动到 Pricing.php 页面吗?

您的主页似乎已经拥有您需要的大部分内容。您只是缺少价格页面所具有的 has_many 的循环。由于您的 MembershipPlans() 函数在 HomePage_Controller returns 所有价格页面中作为 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 -->