我无法设置嵌套has_many关系的SortOrder,其中子项可以通过Orderable Rows的Gridfield Extensions模块进行拖放重新排序。我也在使用Multi Class Editor
我的Page
有很多ComplexStrips
。这些ComplexStrip
个对象有很多' LinkedObjects'。
问题:
当我尝试通过GridField添加ComplexStrip
时,我收到以下错误: Uncaught InvalidArgumentException:无效的sort()列
我的设置:
Page Class: 当我浏览在CMS中编辑它时,这个类工作正常。
<?php
class Page extends SiteTree {
private static $has_many = array(
'ComplexStrips' => 'ComplexStrip'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$dataColumns = new GridFieldDataColumns();
$dataColumns->setDisplayFields(
array(
'Title' => 'Title',
'ClassName' => 'Class Name'
)
);
$multiClassConfig = new GridFieldAddNewMultiClass();
$multiClassConfig->setClasses(
array(
'ComplexStrip'
)
);
$stripsGridField = GridField::create('ComplexStrips', 'Complex strips', $this->Strips(),
GridFieldConfig::create()->addComponents(
new GridFieldToolbarHeader(),
new GridFieldDetailForm(),
new GridFieldEditButton(),
new GridFieldDeleteAction('unlinkrelation'),
new GridFieldDeleteAction(),
new GridFieldOrderableRows('SortOrder'), // This line causes the issue
new GridFieldTitleHeader(),
$multiClassConfig,
$dataColumns
)
);
$fields->addFieldsToTab(
'Root.Strips',
array(
$stripsGridField
)
);
return $fields;
}
}
TextBlocksStrip类:
一旦我尝试通过Page
上的gridfield添加其中一个,我就会收到错误: Uncaught InvalidArgumentException:无效的sort()列
<?php
class ComplexStrip extends DataObject {
private static $db = array(
'SortOrder' => 'Int'
);
private static $has_many = array(
'TextBlocks' => 'TextBlock'
);
//-------------------------------------------- CMS Fields
public function getCMSFields() {
$fields = parent::getCMSFields();
//---------------------- Main Tab
$textBlocks = GridField::create('TextBlocks', 'Text blocks', $this->TextBlocks(),
GridFieldConfig::create()->addComponents(
new GridFieldToolbarHeader(),
new GridFieldAddNewButton('toolbar-header-right'),
new GridFieldDetailForm(),
new GridFieldDataColumns(),
new GridFieldEditButton(),
new GridFieldDeleteAction('unlinkrelation'),
new GridFieldDeleteAction(),
new GridFieldOrderableRows('SortOrder'),
new GridFieldTitleHeader(),
new GridFieldAddExistingAutocompleter('before', array('Title'))
)
);
$fields->addFieldsToTab('Root.Main',
array(
$textBlocks
)
);
return $fields;
}
}
TextBlock类:
<?php
class TextBlock extends DataObject {
private static $db = array(
'SortOrder' => 'Int'
);
private static $default_sort = 'Sort';
private static $has_one = array(
'TextBlocksStrip' => 'TextBlocksStrip'
);
}
如果我删除行new GridFieldOrderableRows('SortOrder')
所有作品但显然无法重新排序。
我把头发拉过这一头。对我来说没有任何意义,为什么这不起作用。
答案 0 :(得分:3)
我做了一个相当优雅的解决方法。
无法查询SortOrder,因为在尝试加入已排序表时父对象ComplexStrip
不存在。因此,将GridFieldOrderableRows包装在if中,以检查父对象是否已分配了ID。
// Need to wait until parent object is created before trying to set sort order
if($this->ID) {
$textBlocks->addComponents(
new GridFieldOrderableRows('Sort')
);
}
之后,一切都与以前在CMS中的工作方式相同。我现在将在模块上打开一个问题,看看是否有更持久的解决方案。
答案 1 :(得分:0)
我遇到与使用$many_many
而非$has_many
相同的错误。我可以看到您使用了$ has_many,因此无法解决您的问题,但可能会帮助其他人遇到与我相同的错误消息。