【发布时间】:2014-07-29 15:02:06
【问题描述】:
我有一个数据对象“矩形”。它与“AttributeValue”(例如 10 英寸)存在多对多关系。数据对象“AttributeValue”也与“Attribute”有很多关系(例如长度(或宽度))。我想在 CMS 中的“矩形”页面上创建一个表格,显示 AttributeValue 和相关的 Attribute:
属性 |价值
长度 | 10英寸
宽度 | 20英寸
颜色 |红色
Cause Attribute 与 Rectangle 无关,我不知道如何获取该表中的数据。该表的值是使用以下代码生成的:
class Rectangle extends Page {
private static $db = array(...);
private static $many_many = array(
'AttributeValue' => 'AttributeValue',
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$config = GridFieldConfig_RelationEditor::create();
$config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'AttributeValue'=>'Value'
));
$attrField = new GridField(
'AttributeValue',
'AttributeValue',
$this->AttributeValue(),
$config
);
$fields->addFieldToTab('Root.Attribute', $attrField);
return $fields;
}
}
class AttributeValue extends DataObject {
private static $db = array('AttributeValue' => 'Varchar');
private static $belongs_many_many = array('Rectangle' => 'Rectangle');
private static $many_many = array('Attribute' => 'Attribute');
}
class Attribute extends DataObject {
private static $db = array('Attribute' => 'Varchar');
private static $belongs_many_many = array('AttributeValue' => 'AttributeValue');
}
编辑:
我有一个 Rectangle 和一些与之相关的 AttributeValues(例如 10 英寸、20 英寸、红色),所以在数据库中有一个表 Rectangle_AttributeValue,它显示了关系。我还有与AttributeValue(表AttributeValue_Attribute)相关的对象Attribute,例如10 英寸 -> 长度,或红色 -> 颜色。所以整个“关系链”看起来像:Rectangle->AttributeValue->Attribute (Rectangle1 -> 10 inch -> length, or Rectangle1 -> red -> color)。我希望我能说得更清楚...
【问题讨论】:
-
不应该
AttributeValue与AttributeType有$has_one关系吗?AttributeValue不能同时是 length 和 width。AttributeType将与AttributeValue建立$has_many关系,但不是多对多。 -
AttributeValue 两者都可以,例如:10 inch 可以是长度值,也可以是宽度值。 (AttributeValue 是一个对象,所以我得到一个表格,其中的值不应出现多次。)
标签: silverstripe