【问题标题】:Silverstripe dataobject to display in multiple pagesSilverstripe 数据对象显示在多个页面中
【发布时间】:2016-03-22 22:58:04
【问题描述】:

我似乎无法弄清楚如何以多种方式使用数据对象。现在我只能让它在一页中显示。

我希望能够编辑 cms 中表格的项目,在一页上显示项目列表,然后在另一页上显示一个特定项目。

这是我到目前为止的结构,它允许我在一个页面中列出所有客户端并在 CMS 中对其进行编辑。我无法在“clientPage”以外的页面上列出它们,也无法看到一个客户的详细视图页面。

class Clients extends DataObject {
 public static $db = array(
    //All the table columns
);

 // One-to-one relationship with profile picture
public static $has_one = array(
    'ProfilePicture' => 'Image',
    'ClientPage' => 'ClientPage'
);

// Summary fields

public static $summary_fields = array(
    'ProfilePicture.CMSThumbnail'=>'Picture',
    'FIRST_NAME'=>'First Name',
    'LAST_NAME'=>'Last Name',
    'EMAIL'=>'Email'
);

public function getCMSFields_forPopup() {

    // Profile picture field
    $thumbField = new UploadField('ProfilePicture', 'Profile picture');
    $thumbField->allowedExtensions = array('jpg', 'png', 'gif');


    // Name, Description and Website fields
    return new FieldList(
        //all the editable fields for the cms popup
    );
}
}

客户页面

class ClientPage extends Page{
    private static $has_many = array(
      'Clients'=>'Client'
    );
    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Client', GridField::create(
            'Client',
            'Client List',
            $this->Clients(),
            GridFieldConfig_RecordEditor::create()
        ));

        return $fields;
    }
}

class ClientPage_Controller extends Page_Controller{
    public function init() {
        parent::init();
    }
}

如果我尝试使用相同的数据对象创建目录页面,它不起作用

class ClientDirectoryPage extends Page {
    private static $has_many = array(
      'Clients'=>'Client'
    );
    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        return $fields;
    }
}

class ClientDirectoryPage_Controller extends Page_Controller{
    public function init() {
        parent::init();
    }
}

【问题讨论】:

    标签: silverstripe data-objects


    【解决方案1】:

    您的代码不起作用,因为您尝试错误地实现 Polymorfic has-one relation

    但是根据您的目标,您应该:

    1. ClientPagehas_one 客户端(然后客户端字段实际上是 ClientPage 字段,作为 1-1 关系)
    2. ClientDirectoryPage 显示一组指向 ClientPages 的链接,并且可以通过多种方式实现关系。

      一个。使用 SiteTree 层次结构:在 ClientDirectoryPage 下放几个 ClientPages,用ClientDirectoryPage::Children()访问列表

      b.在ClientDirectoryPage_Controller::ClientPages() 中以ClientPage::get() 获取所有页面的列表

    【讨论】:

    • 请澄清一下,您为什么需要一个单独的 Client 类?
    • 如果我只想创建一个填充了所提供链接的客户端页面。我有一个已经满是客户的表,不想为每个客户创建一个页面。我想弄清楚如何使用 $datObject::get 来显示表中所有行的列表。你能给我看看?
    • 客户端有多个数据字段,由站点管理员在站点内进行管理。他们不是用户/成员...
    • 如果数据库表包含您的客户数据,您可以在 SS CMS 管理员(在后端)中以两种方式管理此表:作为 SiteTree(页面)字段或 DataObject 字段。您应该使用ModelAdmin 获取最新信息。如果您想在某个页面(在前端)上显示您的客户表,您应该在页面控制器上使用返回客户列表的方法,以及呈现表格的模板部分。
    • 我确实最终使用了 ModelAdmin 和 DataObject,以便可以在需要的地方使用数据。出于某种原因,单击 Silverstripe 如何与数据交互只需要一段时间。谢谢你的帮助。我会将其标记为已回答,因为您的回答将使我朝着正确的方向去做我要求做的事情,尽管我现在会接受您的 ModelAdmin 建议。
    猜你喜欢
    • 1970-01-01
    • 2015-11-13
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    相关资源
    最近更新 更多