SilverStripe - limiting the number of many relations a dataobject can have

瘦欲@ 提交于 2019-12-09 03:39:00

问题


If I have a $has_many relationship that I want to manage with a GridField in the cms, how would I go about putting a limit on the number of how many relations one object can have? Is this possible?

Can I do this in the model or would it have to be a check I add into the GridField I'm using to add and remove relations?

I'm looking at implementing GridField_SaveHandler to make a custom GridFieldComponent but not sure how I can use this to abort the save if i detect something is wrong.


回答1:


the following 2 solutions are not the cleanest way to solve this, but the most pragmatic and easiest to implement.

basically, what I suggest to do, is just count the objects and remove the ability to add new records once the count is above a certain number.

if you want to limit the number of records on a single relation/grid (lets say max 5 players per team):

class Player extends Dataobject {
    private static $db = array('Title' => 'Varchar');
    private static $has_one = array('TeamPage' => 'TeamPage');
}
class TeamPage extends Page {
    private static $has_one = array('Players' => 'Player');
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $config = GridFieldConfig_RecordEditor::create();
        if ($this->Players()->count > 5) {
            // remove the buttons if we don't want to allow more records to be added/created
            $config->removeComponentsByType('GridFieldAddNewButton');
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
        }
        $grid = GridField::create('Players', 'Players on this Team', $this->Players(), $config);
        $fields->addFieldToTab('Root.Main', $grid);
        return $fields;
    }
}

if you want to limit the total number of records globally (if we limit this way to 5, this means if 1 Team already has 3 Players, then the 2nd team can only have 2):

class Player extends Dataobject {
    private static $db = array('Title' => 'Varchar');
    private static $has_one = array('TeamPage' => 'TeamPage');
    public function canCreate($member = null) {
        if (Player::get()->count() > 5) {
           return false;
        }
        return parent::canCreate($member);
    }
}
class TeamPage extends Page {
    private static $has_one = array('Players' => 'Player');
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $config = GridFieldConfig_RecordEditor::create();
        $grid = GridField::create('Players', 'Players on this Team', $this->Players(), $config);
        $fields->addFieldToTab('Root.Main', $grid);
        return $fields;
    }
}



回答2:


I have wrote a quick jQuery plugin to limit the number of items a GridField can have: -

Download the plugin here: - gridfieldlimit.js

https://letscrate.com/f/monkeyben/silverstripe/gridfieldlimit.js

Set up the plugin in the getCMSFields function: -

// Pass GridField configs, each one containing field name and item limit
$vars = array(
    "GridFieldLimits" => "[['GRIDFIELD_NAME_1', 3], ['GRIDFIELD_NAME_2', 6]]",
);

// Load the jquery gridfield plugin
Requirements::javascriptTemplate("themes/YOUR_THEME_NAME/javascript/gridfieldlimit.js", $vars);



回答3:


works for me: make the canCreate method of the DataObject that's managed by your GridField check for existing objects.

of course, this doesn't allow you to implement a customg GridFieldComponent, as you need to modify the DataObject code.



来源:https://stackoverflow.com/questions/19005633/silverstripe-limiting-the-number-of-many-relations-a-dataobject-can-have

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!