0

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

我希望能够编辑 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();
    }
}
4

1 回答 1

0

您的代码不起作用,因为您尝试错误地实现Polymorfic 具有一关系

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

  1. A ClientPagethat has_oneClient(然后 Client 字段实际上是 ClientPage 字段,如 1-1 关系)
  2. AClientDirectoryPage显示指向 ClientPages 的链接集合,并且可以通过多种方式实现关系。

    一个。使用 SiteTree 层次结构:将几个 ClientPages 放在 ClientDirectoryPage 下,并使用ClientDirectoryPage::Children()

    湾。获取所有页面的列表,ClientPage::get()ClientDirectoryPage_Controller::ClientPages()

于 2016-03-23T01:13:50.867 回答