How to customize configureDatagridFilters in Sonata Admin to use non related mongodb documents

本小妞迷上赌 提交于 2020-01-06 02:41:05

问题


In my Mongodb I got a passenger document, this is a typical item:

{
    "_id" : ObjectId("51efdf818d6b408449000002"),
    "createdAt" : 1374674817,
    "phone" : "222222",
    ..
}

I also have a device document that references a passenger document, here is an example:

{
    "_id" : ObjectId("51efdf818d6b408449000001"),
    "os" : "android.gcm",
    "passenger" : ObjectId("51efdf818d6b408449000002"),
    ..
}

so in other words.. there is no way I can find out the device belonging to a passenger by running a query on passenger.. it must be a query on device.

In my PassengerAdmin.php I got this configure list field definition:

public function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name', 'text', array('label' => 'Name'))
        ->addIdentifier('phone', 'text', array('label' => 'Phone #'))
        ->addIdentifier('createdAt', 'datetime', array('label' => 'Created At'))
        ->addIdentifier('device.os', 'text', array('label' => 'Device OS Type')) 
        ..
    ;
}

which works fine (I have no idea how sonata managed to map device.os to passengers.. but oh well).

Inside my configureDataGridFilters this will return an error:

protected function configureDatagridFilters(DatagridMapper $datagrid)
{
    $datagrid->add('device.os');
}

error:

Notice: Undefined index: device.os in ../vendor/sonata-project/doctrine-mongodb-admin-bundle/Sonata/DoctrineMongoDBAdminBundle/Builder/DatagridBuilder.php line 60

which i guess makes sense.. and even if i created that index nothing will be returned.. (by the way I got that idea from here: see displaying subentity properties

question:

how can I customize the filter regarding the device OS version so that it incorporates info related to the Device document. lemme show what I want to get done using this example (mix of code and pseudocode):

->add('osVersion', 'doctrine_mongo_callback', [
    'callback' => function ($queryBuilder, $alias, $field, $params) {
            if ($params['value'] === null) {
                return;
            }
            // for each passengers as passenger
            // get passenger.id = %passengerID%
            // grab device that has passenger = %passengerID%
            // filter so that device.os == $params['value']

    'field_type' => 'choice',
    'field_options' => ['choices'=> ['android.gcm'=> "Android", "os.ios"=>"iOS"]]
]);

I noticed that the createQuery method of the superclass Admin can be overridden.. but it seems that that is globally so, and it wouldn't help me in this specific case.

来源:https://stackoverflow.com/questions/24450039/how-to-customize-configuredatagridfilters-in-sonata-admin-to-use-non-related-mon

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