How to search for a User (ManyToOne) by entering his username in datagrid filter?

240 views
Skip to first unread message

full_metal

unread,
May 22, 2012, 3:41:35 PM5/22/12
to sonata...@googlegroups.com
Hello,

I have the following datagrid filter that works perfectly:-

class TransactionAdmin 
{
...
     $datagridMapper
            ->add('user')
            ->add('status')
    ;
...
}

Transaction has a ManyToOne association with User entity. 

So I get a nice drop down box with a list of all the user which I can select from.
But the problem is that while testing the app, we'll load hundreds of thousands of users in the database and obviously loading a select box with >100,000 options is not a feasible option. 

How can I customize the query such that the administrator can enter the username in an input box and the query runs with a "LIKE" condition. 

I have tried - 
$datagridMapper
            ->add('offer', 'doctrine_orm_string', array(), null, array())

It works if I select Filter Option - 'is equal to' and input the correct user_id in the field. 

I have the following two requirements:-
1. How can I set the FilterOptions to only display 'is equal to' option instead of 'contains', 'does not contain', 'is equal to'
2. How can I use 'username' instead of the 'user_id' for the filter query? 

Thanks,
Amit

niobe

unread,
May 23, 2012, 8:24:16 AM5/23/12
to sonata...@googlegroups.com
A good way to approach this particular problem is making use of the "sonata_type_model" field type. I believe it would implement your requirements as specified.
Your implementation would look as follows

class TransactionAdmin 
{
...   
    public function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('Your admin section for user association')
                ->add('user', 'sonata_type_model', array(), array('edit' => 'list'))
            ->end()
        ;
    }
...
}

This enables to open an embedded form with filter and selection functionalities.
Hope it helps.

PS
If you are going to try and manage(general crud) your transactions from the user form, could you let me know(or post a thread in this group) how you implemented it?
It is one functionality that I am struggling to implement.

niobe

unread,
May 23, 2012, 2:45:17 PM5/23/12
to sonata...@googlegroups.com
I noticed in one of your previous posts you have already tried this. The only thing that might make your implementation different from mine, is that I have used doctrine crud to generate my base entities. I also for some reason used a getName function in what would be in your case TransactionType.php.

    public function getName()
    {
        return 'Acme_DemoBundle_TransactionType';
    }

Thats all I have.

Robert Gründler

unread,
May 24, 2012, 3:29:56 AM5/24/12
to sonata...@googlegroups.com
Hi Amit,

i've solved something similar using the "doctrine_orm_callback". here's a gist which demonstrates it:

https://gist.github.com/2780022


regards

-robert




--
You received this message because you are subscribed to the Google Groups "sonata-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sonata-users/-/k1_8oNYwOVMJ.

To post to this group, send email to sonata...@googlegroups.com.
To unsubscribe from this group, send email to sonata-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sonata-users?hl=en.

full_metal

unread,
May 25, 2012, 3:08:49 AM5/25/12
to sonata...@googlegroups.com
Dear Niobe,

Thanks for the detailed response. Yes, I did try your approach too. 
Right now, I have settled for the performance hit on page load and have use the Chosen JQuery Plugin which will allow me to search the options in the list. 
I defined a __toString() method in my User Entity to return the corresponding username too. 

As for your query about managing CRUD for associated entities there is a array('edit' => 'inline') option which I have not experimented with yet, but read in the docs. 

Hope it helps!

Cheers,
Amit

full_metal

unread,
May 25, 2012, 3:22:06 AM5/25/12
to sonata...@googlegroups.com
Dear Robert!

Thanks a lot. 
I did try writing a callback, but I think my query was wrong and I mistook it for a problem with closures. 
I'll give it another shot. 

Warm Regards,
Amit


On Thursday, 24 May 2012 12:59:56 UTC+5:30, Robert Gründler wrote:
Hi Amit,

i've solved something similar using the "doctrine_orm_callback". here's a gist which demonstrates it:

https://gist.github.com/2780022


regards

-robert



On Wed, May 23, 2012 at 8:45 PM, niobe <pt201...@gmail.com> wrote:
I noticed in one of your previous posts you have already tried this. The only thing that might make your implementation different from mine, is that I have used doctrine crud to generate my base entities. I also for some reason used a getName function in what would be in your case TransactionType.php.

    public function getName()
    {
        return 'Acme_DemoBundle_TransactionType';
    }

Thats all I have.

--
You received this message because you are subscribed to the Google Groups "sonata-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sonata-users/-/k1_8oNYwOVMJ.

To post to this group, send email to sonata...@googlegroups.com.
To unsubscribe from this group, send email to sonata-users+unsubscribe@googlegroups.com.

Lyle Nel

unread,
May 25, 2012, 9:06:48 AM5/25/12
to sonata...@googlegroups.com
Thank you for your advice kind Sir.
Reply all
Reply to author
Forward
0 new messages