Hello,
I'm building an administrative interface for an object with a many-to-many relationship using ModelAdmin. I've encountered a problem with the auto-generated related items table grid: it won't show a list of existing items from the far side of the many-to-many relationship.
Silverstripe Version: "trunk" (http://svn.silverstripe.com/open/phpinstaller/trunk) running on Apache 2.2/Win XP Pro.
Specifics:
A child of ModelAdmin is managing QuoteModel. QuoteModel has a many-to-many relationship with QualityPage. Using the auto-generated ModelAdmin CMS interface, new QuoteModels can be created. However, when pulling up a QuoteModel instance and clicking on the Qualities tab to assign relationships, the displayed table field shows "No items found" even though multiple QualityPage objects are persisted in the database.
The only way I can create a new QuoteModel-QualityPage relationship is by using the add button which creates a new QualityPage. This is undesirable--it is important that the end user be able to select from existing QualityPage instances.
Here's QuoteModel:
<?
class QuoteModel extends DataObject {
static $db = array(
'Quote' => 'Text',
'Source' => 'Varchar',
'Author' => 'Varchar'
);
static $many_many = array(
'Qualities' => 'QualityPage'
);
static $many_many_extraFields = array(
'Qualities' => array('Primary' => 'Boolean')
);
?>
Is there something I should be doing differently to get the table field to auto-generate correctly (showing existing QualityPages)? Or, is this a bug with the auto-generation code?
I've made some progress in finding a solution to this problem. If I add this code to QuoteModel --
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', new TextareaField('Quote'));
$fields->addFieldToTab('Root.Main', new TextField('Source'));
$fields->addFieldToTab('Root.Main', new TextField('Author'));
$tablefield = new ManyManyComplexTableField(
$this,
'Qualities',
'QualityPage',
array(
'Title' => 'Quality'
)
);
$fields->addFieldToTab('Root.Main', $tablefield);
return $fields;
}
-- the list of qualities now appears in a ManyManyComplexTableField with a checkbox by each! This is a step in the right direction.
This solution isn't bug free, though. If a user attempts to create a new QuoteModel and set relationships at the same time (before initially saving the QuoteModel), those relationships will be lost. The user needs to first create the QuoteModel and then (only after saving it) add relationships to it.
Any ideas on how to get QuoteModel to save its relationships during the creation process?
Thank you for your help!
Ben
------
For Reference:
<?
class QuotesAdmin extends ModelAdmin {
protected static $managed_models = array(
'QuoteModel'
);
static $url_segment = 'quotes';
static $menu_title = 'Quote Admin';
}
?>