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

GridFieldEditableColumns does not work with ManyManyThroughList #309

Closed
ntd opened this issue Jun 9, 2020 · 3 comments
Closed

GridFieldEditableColumns does not work with ManyManyThroughList #309

ntd opened this issue Jun 9, 2020 · 3 comments

Comments

@ntd
Copy link
Contributor

ntd commented Jun 9, 2020

I just tried to substitute the two occurrences of $list instanceof ManyManyList with ($list instanceof ManyManyList || $list instanceof ManyManyThroughList) but it does not seem to work properly.

Let's pretend to have the following data model:

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 = [
        'SomeData' => 'Varchar(10)',
    ];

    private static $has_one = [
        'Team' => Team::class,
        'Supporter' => Supporter::class,
    ];
}

With the following code in Team::getCMSFields (that works properly when SomeData is defined as $many_many_extraFields), I am able to change it (I see the new value in the database) but the form field element is not properly populated (it is always empty):

$config->getComponent>ByType(GridFieldEditableColumns::class)->setDisplayFields([
    'SomeData' => 'A really useful field',
]);

With the following code, the form field is properly populated but it is turned readonly:

$config->getComponent>ByType(GridFieldEditableColumns::class)->setDisplayFields([
    'TeamSupporter.SomeData' => 'A really useful field',
]);

Unfortunately I need to use an explicit through relation to be able to populateDefaults() the extra fields and add some calculated field. If anyone has a better idea I'm all ears.

ntd added a commit to ntd/silverstripe-gridfieldextensions that referenced this issue 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`.
@ntd
Copy link
Contributor Author

ntd commented Jun 10, 2020

I just opened a PR that allows me to use the first approach, i.e.:

$config->getComponent>ByType(GridFieldEditableColumns::class)->setDisplayFields([
    'SomeData' => 'A really useful field',
]);

@ntd
Copy link
Contributor Author

ntd commented Jun 24, 2020

The recently merged #294, together with silverstripe/silverstripe-framework#9192, will allow to properly use dot syntax in this cases, e.g. the following code should work:

$config->getComponent>ByType(GridFieldEditableColumns::class)->setDisplayFields([
    'TeamSupporter.SomeData' => 'A really useful field',
]);

Although not released, one can leverage cweagans/composer-patches to apply unmerged changes, e.g. by having a variant of the following text in your composer.json:

{
  ...
  "require": {
    ...
    "cweagans/composer-patches": "^1.0"
  },
  "extra": {
    "patches": {
      "silverstripe/framework": {
        "Support dot syntax in ManyManyThroughList": "https://github.com/silverstripe/silverstripe-framework/pull/9192.patch"
      }
    }
  }
}

@ntd
Copy link
Contributor Author

ntd commented May 23, 2021

silverstripe/silverstripe-framework#9192 has been recently merged, so I consider this issue solved.

@ntd ntd closed this as completed May 23, 2021
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

No branches or pull requests

1 participant