具有has_many关系的ModelAdmin

时间:2016-09-15 11:04:22

标签: php silverstripe modeladmin

我正在ModelAdmin扩展DataObject来管理$has_many,并且我有DataObject关系由另一个class Applications extends ModelAdmin { private static $menu_title = 'Applications'; private static $url_segment = 'apps'; private static $managed_models = array ( 'Application' ); } 管理。我想在另一个标签页上管理此对象,但我不知道如何添加它。我的基本代码:

ApplicationAdmin.php:

class Application extends DataObject {
    private static $db = array(
        'Name' => "varchar"
    );

    private static $has_many = array(
        'Documents' => 'Document',
    );

    public function getCMSFields() {
        $fields = FieldList::create(
            TextField::create('Name'),
        );

        return $fields;
    }
}

Application.php

class Document extends DataObject {
    private static $db = array(
        'Title' => "varchar",
    );

    private static $has_one = array(
        "Document" => "File",
        "Application" => "Application"
    );

    public function getCMSFields () {
        $fields = FieldList::create(
            TextField::create('Title'),
            $doc = new UploadField('Document')
        );
        $doc->getValidator()->setAllowedExtensions(array('pdf', 'docx'));

        return $fields;
    }   
}

Document.php

Root.Documents

基本上我想在<?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { /** * Other code goes here **/ public function itemDestribution() { return $this->hasMany('App\ItemDistribution','foreign_key'); } } 标签下管理此条目的文档。

1 个答案:

答案 0 :(得分:2)

您可以使用GridField来处理ApplicationDocument之间的关系,并根据需要将该字段放在自己的标签上。例如:

# Application.php
public function getCMSFields() {

    $fields         = parent::getCMSFields();
    $nameField      = TextField::create('Name');
    $documentsField = GridField::create(
        'Documents',
        'Documents',
        $this->Documents(),
        GridFieldConfig_RelationEditor::create()
    );

    $fields->addFieldToTab(
        'Root.Main',
        $nameField
    );

    $fields->addFieldToTab(
        'Root.Documents',
        $documentsField
    );

    return $fields;

}

您的代码在Document类中包含一些拼写错误(类名中包含.php,getValidator应为getValidator()),该类还需要$has_one = ['Application' => 'Application'];为了工作的关系。