Issues with Using Override method on UserModel Search
As part of this large ongoing project, I need to create a members list where you can search the members. I've got the basics of that all up and working, however there are quite a few extra fields in the user database (Such as Real Name) which will also need to be searched by.
Unfortunately, the UserModel Search method has no events I could hook into to include the extra fields in the array (line 1950), so I was attempting to use the override method to add it in to avoid editing core code, however it doesn't change anything.
Any ideas on how to approach this? I'd prefer not to just create a new method for it; That's a lot of duplicate code for 1 line, and also would mean that other parts of the site using the Search function still wouldn't be able to search by real name, which is really the most crucial one here.
Thanks
EDIT: Oh, and of course the same to the SearchCount method.
Best Answers
-
hgtonight MVP
I can see doing this in a few ways:
- Create your own usermodel class that extends
UserModel
. Directly override the Search and SearchCount methods in this class. Then register your modified usermodel in the bootstrap before. - Create 2 new magic methods (e.g. SearchPlus, and SearchPlusCount) and use those wherever you want.
- Modify the core files by firing an event where needed, then putting your custom code in a plugin
A quick search of the 2.1 source shows the
UserModel::Search
andUserModel::SearchCount
methods are only used once in the core code.Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
6 - Create your own usermodel class that extends
-
hgtonight MVP
If you are creating a magic method on a pluggable class, you can get the passed arguments via the RequestArgs member of the sending object.
E.g. I create in my plugin class a method called
UserModel_SearchE_Create($Sender)
and call it via$UserModel->SearchE($Filter, '', 'asc', $Limit, $Offset)
. In my plugin file, I can get the contents of the call's arguments via$Sender->RequestArgs
. Use something like$Filter = val(0,$Sender->RequestArgs, FALSE);
where 0 is the arg number (starting at 0), and FALSE is the default value if the argument wasn't passed.Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
5
Answers
I have also just attempted to do it via creating a new method and for some reason it's not getting any of the variables passed to it... Totally confused now. Am I just going to have to create an extension of the user model to do this?
Just pass an array into the Search method for the
$Keywords
parameter. If that is an array, it gets passed into theGdn_SQL->Where()
method.Or am I being completely naive here?
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Yeah I have thought of that as a last resort, and also probably what I shall do for some of the other fields. Would passing the array result in it still doing a like check, or would it try to do an equals search? For instance if searching for "Jon" would it find only "Jon" or "Jon Smith, Jon Paul" etc.
Ideally, the best thing would be to have an event hook added into those two methods so that extra fields could be added to it. We want to make sure that any times the UserModel()->Search() is called in it's most basic method (ie passing it a simple keyword as a string) that it searches for the Real Name as well as the Username and Email.
I hope that makes sense, I'm not sure if it does...
I can see doing this in a few ways:
UserModel
. Directly override the Search and SearchCount methods in this class. Then register your modified usermodel in the bootstrap before.A quick search of the 2.1 source shows the
UserModel::Search
andUserModel::SearchCount
methods are only used once in the core code.Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Yeah that's what I was thinking, unfortunately my custom search methods aren't receiving any of the variables I'm passing, they're just remaining as the defaults. Not sure if there's anything you can see wrong with this?
$this->Members = $this->UserModel->SearchE($Filter, '', 'asc', $Limit, $Offset)->Result();
I guess if it's not used more than once (and that one instance is on the admin section to my knowledge) then it shouldn't be much of a concern, however I would expect it to be used by the mentions plugin amongst others, so I guess I'll have to update them too.
It looks like the only available method will be created a new model extending the standard user model. Is there a process for requesting events to be added to the core?
Thanks for all the help.
EDIT: Okay just checked, it isn't apparently used in any of the plugins, so looks like I'll have to edit the mentions plugin manually anyway. I've gone with simply extending the class for the time being.
If you are creating a magic method on a pluggable class, you can get the passed arguments via the RequestArgs member of the sending object.
E.g. I create in my plugin class a method called
UserModel_SearchE_Create($Sender)
and call it via$UserModel->SearchE($Filter, '', 'asc', $Limit, $Offset)
. In my plugin file, I can get the contents of the call's arguments via$Sender->RequestArgs
. Use something like$Filter = val(0,$Sender->RequestArgs, FALSE);
where 0 is the arg number (starting at 0), and FALSE is the default value if the argument wasn't passed.Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Thankyou! That is exactly what I was looking for I've already started doing it in a model extension, so I'll probably stick with it for now, but that'll most likely save me in future. Most confused because I don't remember having used it on any of the previous methods I've created (And there are so damn many that I've ended up dividing them between files by now just so I know where to find what I'm looking for)