Cockpit: [Help] Problems filtering API content.

Created on 24 Feb 2018  路  4Comments  路  Source: agentejo/cockpit

To start excellent CMS - API. I congratulate you.

I have problems to be able to make filters in the API using filter.

In the following example I have this answer:

{
    "fields": {
        "titulo": {
            "name": "titulo",
            "type": "text",
            "localize": false,
            "options": []
        },
        "telefono": {
            "name": "telefono",
            "type": "repeater",
            "localize": true,
            "options": {
                "text": ""
            }
        },
        "Comuna": {
            "name": "Comuna",
            "type": "collectionlink",
            "localize": false,
            "options": {
                "link": "regiones_ciudades",
                "multiple": false
            }
        }
    },
    "entries": [
        {
            "titulo": "Jardines",
            "imagen": {
                "path": "_01.png"
            },
            "_mby": "5a91a698c177fdoc407958704",
            "_by": "5a91a698c177fdoc407958704",
            "_modified": 1519499982,
            "_created": 1519495090,
            "_id": "5a91a7b22d661doc939554262",
            "telefono": [
                {
                    "field": {
                        "type": "text"
                    },
                    "value": 1111
                },
                {
                    "field": {
                        "type": "text"
                    },
                    "value": 22222
                },
                {
                    "field": {
                        "type": "text"
                    },
                    "value": 3333
                }
            ],
            "test": null,
            "Galeria": [
                {
                    "meta": {
                        "title": "",
                        "asset": "5a91a9f40748bdoc1987853657"
                    },
                    "path": "/storage/uploads/2018/02/24/5a91a9f3eba85_03.png"
                },
                {
                    "meta": {
                        "title": "",
                        "asset": "5a91a9f3e9a67doc624470269"
                    },
                    "path": "/storage/uploads/2018/02/24/5a91a9f3d857a_02.png"
                },
                {
                    "meta": {
                        "title": "",
                        "asset": "5a91a9f3d63e9doc839294720"
                    },
                    "path": "/storage/uploads/2018/02/24/5a91a9f3c4098_01.png"
                }
            ],
            "multplies": null,
            "Comuna": {
                "_id": "5a91acceacadcdoc963333767",
                "link": "regiones_ciudades",
                "display": "Santiago"
            },
            "tags": [
                "dfsdsf",
                "rrrere",
                "rretterret",
                "eewrwer+"
            ]
        },
        {
            "telefono": [
                {
                    "field": {
                        "type": "text"
                    },
                    "value": 1111
                }
            ],
            "titulo": "Jardines 2",
            "imagen": {
                "path": "_01.png"
            },
            "test": "",
            "Galeria": [
                {
                    "meta": {
                        "title": "",
                        "asset": "5a91a9f3d63e9doc839294720"
                    },
                    "path": "/storage/uploads/2018/02/24/5a91a9f3c4098_01.png"
                },
                {
                    "meta": {
                        "title": "",
                        "asset": "5a91a9f40748bdoc1987853657"
                    },
                    "path": "/storage/uploads/2018/02/24/5a91a9f3eba85_03.png"
                },
                {
                    "meta": {
                        "title": "",
                        "asset": "5a91a9f41910fdoc1608599129"
                    },
                    "path": "/storage/uploads/2018/02/24/5a91a9f40940500.jpg"
                }
            ],
            "_mby": "5a91a698c177fdoc407958704",
            "_by": "5a91a698c177fdoc407958704",
            "_modified": 1519503534,
            "_created": 1519495701,
            "_id": "5a91aa15e7da6doc1164984544",
            "multplies": null,
            "Comuna": null,
            "tags": null
        }
    ],
    "total": 2
}

When filtering using this address the content is filtered without problems (ID Filter)
"localhost:8080/api/collections/get/jardines?token=b2dd75dbbe2f31eda473b9af14e94e&filter[_id]=5a91a7b22d661doc939554262"

{
    "fields": {
        "titulo": {
            "name": "titulo",
            "type": "text",
            "localize": false,
            "options": []
        },
        "telefono": {
            "name": "telefono",
            "type": "repeater",
            "localize": true,
            "options": {
                "text": ""
            }
        },
        "Comuna": {
            "name": "Comuna",
            "type": "collectionlink",
            "localize": false,
            "options": {
                "link": "regiones_ciudades",
                "multiple": false
            }
        }
    },
    "entries": [
        {
            "titulo": "Jardines",
            "imagen": {
                "path": "_01.png"
            },
            "_mby": "5a91a698c177fdoc407958704",
            "_by": "5a91a698c177fdoc407958704",
            "_modified": 1519499982,
            "_created": 1519495090,
            "_id": "5a91a7b22d661doc939554262",
            "telefono": [
                {
                    "field": {
                        "type": "text"
                    },
                    "value": 1111
                },
                {
                    "field": {
                        "type": "text"
                    },
                    "value": 22222
                },
                {
                    "field": {
                        "type": "text"
                    },
                    "value": 3333
                }
            ],
            "test": null,
            "Galeria": [
                {
                    "meta": {
                        "title": "",
                        "asset": "5a91a9f40748bdoc1987853657"
                    },
                    "path": "/storage/uploads/2018/02/24/5a91a9f3eba85_03.png"
                },
                {
                    "meta": {
                        "title": "",
                        "asset": "5a91a9f3e9a67doc624470269"
                    },
                    "path": "/storage/uploads/2018/02/24/5a91a9f3d857a_02.png"
                },
                {
                    "meta": {
                        "title": "",
                        "asset": "5a91a9f3d63e9doc839294720"
                    },
                    "path": "/storage/uploads/2018/02/24/5a91a9f3c4098_01.png"
                }
            ],
            "multplies": null,
            "Comuna": {
                "_id": "5a91acceacadcdoc963333767",
                "link": "regiones_ciudades",
                "display": "Santiago"
            },
            "tags": [
                "dfsdsf",
                "rrrere",
                "rretterret",
                "eewrwer+"
            ]
        }
    ],
    "total": 1
}

But when I want to filter using a multiple type field I do not get results (Phone Fields):

"localhost:8080/api/collections/get/jardines?token=b2dd75dbbe2f31eda473b9af14e94e&filter[telefono[value]]=1111"

{
    "fields": {
        "titulo": {
            "name": "titulo",
            "type": "text",
            "localize": false,
            "options": []
        },
        "telefono": {
            "name": "telefono",
            "type": "repeater",
            "localize": true,
            "options": {
                "text": ""
            }
        },
        "Comuna": {
            "name": "Comuna",
            "type": "collectionlink",
            "localize": false,
            "options": {
                "link": "regiones_ciudades",
                "multiple": false
            }
        }
    },
    "entries": [],
    "total": 0
}

Thanks for the support

Most helpful comment

Hi,

if you're using mongodb as cockpits data source then this filter query should work:

filter[telefono][value]=1111

but if you're using the default MongoLite data source then querying nested values isn't supported, sorry.

a workaround is to create a custom api entry point. to do so create the following file /config/api/telefono.php

we now do a custom query in that file:

<?php

$value = $this->param('value');

if (!$value) {
    return $value;
}

$entries = cockpit('collections')->find('jardines', [
    'filter' => function($document) use ($value) {

        foreach((array)$document['telefono'] as $item)  {
              if ($item['value'] === $value)  return true;
        } 

        return false;
    }
]);

return $entries;

now you can query localhost:8080/api/telefono?token=xxx&value=1111

caution! not tested ;-)

All 4 comments

Hi,

if you're using mongodb as cockpits data source then this filter query should work:

filter[telefono][value]=1111

but if you're using the default MongoLite data source then querying nested values isn't supported, sorry.

a workaround is to create a custom api entry point. to do so create the following file /config/api/telefono.php

we now do a custom query in that file:

<?php

$value = $this->param('value');

if (!$value) {
    return $value;
}

$entries = cockpit('collections')->find('jardines', [
    'filter' => function($document) use ($value) {

        foreach((array)$document['telefono'] as $item)  {
              if ($item['value'] === $value)  return true;
        } 

        return false;
    }
]);

return $entries;

now you can query localhost:8080/api/telefono?token=xxx&value=1111

caution! not tested ;-)

Thank you very much Arthur.

Change to mongo db, and it worked correctly.

Dear Arthur,

is this information still up-to-date?

but if you're using the default MongoLite data source then querying nested values isn't supported, sorry.

I need to list a collection filtered by a collectionlink field. My first approach was to filter this by the _id, but this this nested—like the other objects link and display too.

So need I really switch to MongoDB? A first try with the docker-compose.yml from the Docu fails. This restrains me to go on with MongoDB too. Besids the data migration.

Do you have a further hint?

Thanks in advance
  Valentin

Are nested queries still not supported in mongolite?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

victormoretti picture victormoretti  路  5Comments

TechnicallyJoe picture TechnicallyJoe  路  6Comments

HausOfAlejandro picture HausOfAlejandro  路  4Comments

tsimenis picture tsimenis  路  5Comments

dragosdydy picture dragosdydy  路  3Comments