How to customize configureDatagridFilters in Sonat

2019-08-07 02:29发布

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.

0条回答
登录 后发表回答