Skip to content

[charcoal/admin] Merge errors between SearchWidget and CollectionContainerTrait #72

Description

@mcaskill

This issue and its proposed hack might be useful if you have multiple collection views but want to limit search results to only that view’s filters.

Example

Given the following classic model metadata collection config:

{
    "properties": [
        "id",
        "name",
        "active"
    ],
    "filters": [
        {
            "property": "master",
            "operator": "IS NOT NULL"
        }
    ]
}

The current behaviour of array_replace_recursive() along with search query filters from SearchWidget results in the following collection config filters:

[
    {
        "property": "master",
        "operator": "IS NULL",
        "filters": [
            {
                "conjunction": "OR",
                "filters": [
                    {
                        "property": "id",
                        "operator": "LIKE",
                        "value": "%hello%"
                    },
                    {
                        "property": "name",
                        "operator": "LIKE",
                        "value": "%hello%"
                    },
                    {
                        "property": "active",
                        "operator": "LIKE",
                        "value": "%hello%"
                    }
                ]
            }
        ]
    }
]

When the SQL query is generated from these filters the nested filters are prioritized over the master condition. This is because each filter is single-purpose: either a group of conditions or a single condition.

Solution 1

The following hack can be used as a temporary solution in your model metadata:

{
    "properties": [
        "id",
        "name",
        "active"
    ],
    "filters": [
        {
            "__SEARCH_WIDGET_PLACEHOLDER__": true
        },
        {
            "property": "master",
            "operator": "IS NOT NULL"
        }
    ]
}
[
    {
        "__SEARCH_WIDGET_PLACEHOLDER__": true,
        "filters": [
            {
                "conjunction": "OR",
                "filters": [
                    {
                        "property": "id",
                        "operator": "LIKE",
                        "value": "%hello%"
                    },
                    {
                        "property": "name",
                        "operator": "LIKE",
                        "value": "%hello%"
                    },
                    {
                        "property": "active",
                        "operator": "LIKE",
                        "value": "%hello%"
                    }
                ]
            }
        ]
    },
    {
        "property": "master",
        "operator": "IS NULL"
    }
]

Solution 2

The following alternative can be used in your model metadata for a less hacky appearance:

{
    "properties": [
        "id",
        "name",
        "active"
    ],
    "filters": {
        "master": {
            "property": "master",
            "operator": "IS NOT NULL"
        }
    }
}
{
    "master": {
        "property": "master",
        "operator": "IS NULL"
    },
    "0": {
        "filters": [
            {
                "conjunction": "OR",
                "filters": [
                    {
                        "property": "id",
                        "operator": "LIKE",
                        "value": "%hello%"
                    },
                    {
                        "property": "name",
                        "operator": "LIKE",
                        "value": "%hello%"
                    },
                    {
                        "property": "active",
                        "operator": "LIKE",
                        "value": "%hello%"
                    }
                ]
            }
        ]
    }
}

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions