php - Silverstripe 从 CheckboxSetField 值创建无序列表 (many_many)

标签 php mysql silverstripe

我正在学习,但我遇到了另一堵墙,所以我有另一个问题......

因此,我创建了一个包含公司所有计划功能的 DataObject,并将所有这些功能显示为带有 CheckboxSetField 的复选框。每当创建新的成员(member)计划类型时,用户就不必纠结 HTML 编辑器字段。

所有这些都已设置且工作正常,唯一的麻烦是,当我询问复选框的值以显示计划功能列表时,我得到一个逗号分隔的列表...这并不符合我的需要能够获取每个选定的功能并将其显示为列表项 ( <li> ... </li> )。

现在它返回诸如 1 ,22 ,3 之类的列表,它们是所选计划功能的 ID。我犹豫是否将该复选框的值设置为计划功能的文本字符串,因此我打算使用这些 ID 来引用 PlanFeature 中的记录。 table 。有更好的方法吗?我想我至少需要一个控件来爆炸这个列表......

我花了几个小时尝试(但失败了)让它工作,我想我需要一些帮助。由于我不知道如何从该列表中获取项目,因此我也没有开始探索如何在模板中显示内容,因此我可能也需要一些帮助。预先感谢:)

这是我的代码。

PlanFeature.php

<?php

class PlanFeature extends DataObject {
    private static $db = array (
        'PlanFeatureText' => 'Varchar',
    );

    private static $belongs_many_many = array (
        'PlanFeature' => 'Pricing',
    );

    private static $summary_fields = array (
        'ID' => 'ID',
        'PlanFeatureText' => 'Plan Feature',
    );

    public function getCMSFields() {
        $fields = FieldList::create(
            TextField::create('PlanFeatureText')
        );
        return $fields;
    }
}

ServicePlan.php 在 getCMSFields()

..//

CheckboxSetField::create(
    'PlanFeatures',
    'Choose Your Plan Features',
    PlanFeature::get()->map('ID', 'PlanFeatureText')
),

..//

定价.php

<?php
class Pricing extends Page {
private static $has_many = array (
    'ServicePlans' => 'ServicePlan'
);

private static $many_many = array (
    'PlanFeatures' => 'PlanFeature'
);

public function getCMSFields() {
    $fields = parent::getCMSFields();

    $fields->addFieldsToTab('Root.Plans', GridField::create(
        'ServicePlans',
        'Orenda Force Service Plan Information',
        $this->ServicePlans(),
        GridFieldConfig_RecordEditor::create()
    ));
    $fields->addFieldsToTab('Root.Plans', GridField::create(
        'PlanFeatures',
        'Manage Plan Features',
        $this->PlanFeatures(),
        GridFieldConfig_RecordEditor::create()
    ));

    return $fields;
    }
}

class Pricing_Controller extends Page_Controller {

}

这就是我完全迷失的地方......所以我确信这些都不正确:(

Page.php (在 Page_Controller 中)

function ExplodedFeatures() {
    $set = new DataObjectSet();

    if($Features = Pricing::get()->First()->PlanFeatures) {
        foreach(explode(',',$Features) as $key => $value) {
            $set->push(new ArrayData(array('Value' => $value)));
        }
    }

echo $set;
}

public function Features() {
    $Feature = Pricing::get();
    $Feature = $Feature->PlanFeatures()->getOptions();
    return $Feature;
    //return $Feature;
}

定价.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>
                    <% loop $PlanFeatures %>
                      <li>$PlanFeatureText</li>
                    <% end_loop %>
                    </ul>
                </div>
            </div>
        <% end_loop %>
        $Content
    </div>
</section>

最佳答案

关于php - Silverstripe 从 CheckboxSetField 值创建无序列表 (many_many),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29584213/

相关文章:

php - 使用PHP在mysql更新中影响的行

javascript - SilverStripe/Entwine Hook 操作保存按钮

php - 如何显示 Cookie?

php - 对于 MySQL PDO 查询,使用 % 和 bindValue 进行 LIKE

php - php 中的消息未打印在浏览器上?

.htaccess - 在 SilverStripe 3.1 中使用 htaccess 的子域出现 500 内部服务器错误

silverstripe - 如何在 SilverStripe 中的主题文件夹内创建公共(public)文件夹

php - 在 Node.js 中模仿 PHP 的 __get(), __set() & __call() 魔术方法

php - 当通过 cli 命令抛出异常时,Symfony 3.3 获取堆栈跟踪(例如服务器 :start)

mysql - 哪种 Ruby ORM 更好或更适合使用?