我有两个不同的数据对象(StockExchangeShare和NewsArticle),它们与many_many关系链接。
在NewsArticle.php中:
private static $many_many = [
'Shares' => StockExchangeShare::class
];
在StockExchangeShare.php中:
private static $belongs_many_many = [
'NewsArticles' => NewsArticle::class
];
在添加新的新闻文章时,用户应该能够将一些现有的证券交易所股票链接到新文章。这应该使用带有配置' GridFieldConfig_RelationEditor'的GridField来完成。
目前,这只适用于已经创建新闻文章的情况。我无法通过网格字段将共享添加到新的(未保存的)文章中。这是错误消息:
E_RECOVERABLE_ERROR:传递给SilverStripe的参数1 \ ORM \ DataList :: subtract()必须是SilverStripe \ ORM \ DataList的实例,给出SilverStripe \ ORM \ UnsavedRelationList的实例,在/ vendor / silverstripe / framework / src /中调用第247行的Forms / GridField / GridFieldAddExistingAutocompleter.php
这是我用来创建网格字段的代码:
$gridFieldConfig = GridFieldConfig_RelationEditor::create();
$gridFieldConfig->removeComponentsByType(GridFieldAddNewButton::class);
$gridFieldConfig->getComponentByType(GridFieldAddExistingAutocompleter::class)->setSearchFields(array('name', 'tickerSymbol', 'isin', 'wpknr'));
$gridFieldConfig->getComponentByType(GridFieldAddExistingAutocompleter::class)->setResultsFormat('$name | $isin');
$fields->addFieldsToTab('Root.Main', [
GridField::create(
'Shares',
'Shares',
$this->Shares(),
$gridFieldConfig
),
]);
这是SilverStripe的问题还是我做错了什么?
答案 0 :(得分:2)
这也与SilverStripe 3中的行为相同。通常我所做的就是在显示GridField之前检查记录是否已保存,否则添加一条消息,说明它在您保存后将可用。
public function updateCMSFields(FieldList $fields)
{
if (!$this->owner->isInDB()) {
$fields->addFieldToTab('Root.Main', LiteralField::create('Please note: you can modify relations when this item has been saved.'));
return;
}
// add your GridField now
}