Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #309 Support ManyManyThroughList in EditableColumns #311

Closed
wants to merge 1 commit into from

Conversation

ntd
Copy link
Contributor

@ntd ntd commented Jun 10, 2020

The ManyManyThrough fields must be specified without comma, hence its
name must be unique in the joined set of fields. For example, given the
following DataObjects:

class Team extends DataObject
{
    private static $many_many = [
        "Supporters" => [
            'through' => TeamSupporter::class,
            'from' => 'Team',
            'to' => 'Supporter',
        ]
    ];
}
class Supporter extends DataObject
{
    private static $belongs_many_many = [
        'Supports' => Team::class,
    ];
}
class TeamSupporter extends DataObject
{
    private static $db = [
        'Ranking' => 'Int',
    ];
    private static $has_one = [
        'Team' => Team::class,
        'Supporter' => Supporter::class,
    ];
}

you should refer to the ranking field with Ranking, not with
TeamSupporter.Ranking.

The ManyManyThrough fields must be specified without comma, hence its
name must be unique in the joined set of fields. For example, given the
following DataObjects:
```
class Team extends DataObject
{
    private static $many_many = [
        "Supporters" => [
            'through' => TeamSupporter::class,
            'from' => 'Team',
            'to' => 'Supporter',
        ]
    ];
}
class Supporter extends DataObject
{
    private static $belongs_many_many = [
        'Supports' => Team::class,
    ];
}
class TeamSupporter extends DataObject
{
    private static $db = [
        'Ranking' => 'Int',
    ];
    private static $has_one = [
        'Team' => Team::class,
        'Supporter' => Supporter::class,
    ];
}
```
you should refer to the ranking field with `Ranking`, not with
`TeamSupporter.Ranking`.
@nyeholt
Copy link
Contributor

nyeholt commented Jun 16, 2020

Hi Nicola, does https://github.com/symbiote/silverstripe-gridfieldextensions/pull/294/files fix your issue too? I'm looking to merge that in as it also fixes an issue with the way loadDataFrom executes during save, but it may also fix your problem if you're able to check and confirm for me?

@ntd
Copy link
Contributor Author

ntd commented Jun 16, 2020

Hi @nyeholt,

unfortunately I am still unable to edit my extra fields. For your convenience I paste here the full-blown code that I used for testing.
The data model is the classic Team-Supporter relation and I want to edit the rankings.

use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\ReadonlyField;
use SilverStripe\ORM\DataObject;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;

class TestAdmin extends ModelAdmin
{
    private static $url_segment = 'editable-test';
    private static $managed_models = [
        Team::class,
    ];
}

class Team extends DataObject
{
    private static $db = [ 'TeamName' => 'Varchar(100)' ];
    private static $summary_fields = [ 'TeamName' ];
    private static $many_many = [
        'Supporters' => [
            'through' => TeamSupporter::class,
            'from' => 'Team',
            'to' => 'Supporter',
        ]
    ];

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $grid = $fields->fieldByName('Root.Supporters.Supporters');
        if ($grid) {
            // Needed: Readonly team + Readonly supporter + Editable ranking

            // (1) Implicit scope: ('none') + Editable supporter + ('none')
            //$display = [
            //    'TeamName' => 'Team',
            //    'SupporterName' => 'Supporter',
            //    'Ranking' => 'Ranking',
            //];

            // (2) Explicit scope: Readonly team + Editable supporter + Readonly ranking
            //$display = [
            //    'TeamSupporter.Team.TeamName' => 'Team',
            //    'SupporterName' => 'Supporter',
            //    'TeamSupporter.Ranking' => 'Ranking',
            //];

            // (3) Handcrafted: Readonly team + Readonly supporter + Editable ranking
            //     Almost there *but* in this case the rankings are not
            //     properly saved!
            $display = [
                'TeamSupporter.Team.TeamName' => 'Team',
                'SupporterName' => [
                    'title' => 'Supporter',
                    'field' => ReadonlyField::class,
                ],
                'TeamSupporter.Ranking' => [
                    'title' => 'Supporter',
                    'field' => NumericField::class,
                ],
            ];

            $config = GridFieldConfig::create()
                ->addComponent(new GridFieldButtonRow('before'))
                ->addComponent(new GridFieldAddNewButton('buttons-before-left'))
                ->addComponent(new GridFieldEditableColumns())
                ->addComponent(new GridFieldDetailForm(null, false, true));
            $config->getComponentByType(GridFieldEditableColumns::class)
                ->setDisplayFields($display);
            $grid->setConfig($config);
        }

       return $fields;
    }
}

class Supporter extends DataObject
{
    private static $db = [ 'SupporterName' => 'Varchar(100)' ];
    private static $belongs_many_many = [ 'Supports' => Team::class ];
    private static $summary_fields = [ 'SupporterName' ];
}

class TeamSupporter extends DataObject
{
    private static $db = [ 'Ranking' => 'Int' ];
    private static $has_one = [
        'Team' => Team::class,
        'Supporter' => Supporter::class,
    ];
}

@sminnee
Copy link

sminnee commented Jun 24, 2020

It's possible that you also need this unmerged change, @ntd, which I pull into my project via composer-patches silverstripe/silverstripe-framework#9192

I can't recall if it was only needed for detail forms or for editable gridfields too, sorry. But give it a go, maybe?

@ntd
Copy link
Contributor Author

ntd commented Jun 24, 2020

Yessss, thank you @sminnee: your PR fixes the issue.

@ntd ntd closed this Jun 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants