1

I am currently building a CRUD based REST API out of an existing CakePHP 3.8 application and am working through refining the results against my CRUD endpoints. I see that query strings are the RESTful way of doing this in ansers like this one, for example using queries like this for searching for color, type and doors against a cars resource:

/cars?color=blue&type=sedan&doors=4

I could hard code something like the following pseudo code:

// Binding would be used to handle sql injection in actual code, but this demonstrates my point
$getParameters = $this->request->getQuery();

$query = $this->CarsTable->find('all');

if(array_key_exists('color', $getParameters) {
    $query->andwhere(['Cars.color like' => $getParameters['color']]));  
} 

if(array_key_exists('type', $getParameters) {
    $query->andwhere(['Cars.type like' => $getParameters['type']]));  
} 

if(array_key_exists('doors', $getParameters) {
    $query->andwhere(['Cars.doors' => $getParameters['doors']]));  
} 

But I have a lot of complex objects that this is going to cover and it seems like there would be a more streamlined approach out there for handling this?

I am wondering what would be the most ideal (or at least a more ideal) method of handling this on the query building / CakeORM integration side of things? Or is there a good plugin for streamlining these sorts of queries in CakePHP?

I have been looking at FriendsOfCake/search, which I know is a search tool. But it seems like it may support the type of functionality I am looking at.

Although if there is something more specifically built for transforming query strings into Cake orm queries, I would in learning what that tool is?

Thanks in advance for the help!

space97
  • 153
  • 1
  • 4
  • 19
  • 1
    If the handling of all the parameters is identical, then a `foreach (['color', 'type', 'doors'] as $param])` loop would seem to shorten this quite a bit. Don't know whether the search tool will do it any more succinctly. – Greg Schmidt May 28 '20 at 17:36
  • Yes, I actually am using a foreacch in my code but it was a little too dynamic for the sake of the code sample I posted. Not sure if I am going to run into issues with identifying and querying different data types in that loop though. – space97 May 28 '20 at 17:39
  • 1
    There's nothing wrong with building a query like that. What exactly are the doubts that you are having? Sure you could abstract this away, similar to how `friendsofcake/search` does it, ie write down search configuration, that is fields and details about what type of search should be performed for that field, etc., is that what you would mean by "_more streamlined_"? From your example code this is certainly something that the search plugin can handle. – ndm Jul 29 '20 at 17:59
  • @ndm The API that I am working on exposes many tables, each containing a lot of fields. Using the example above as a pattern would be possible, but cumbersome to maintain in my use case. I was hoping that there was some kind of community based functionality for abstracting this in Cake, rather than having to write something like that myself. The search plugin seemed like it could possibly be leveraged for this type of used case, since it seems like the core functionality would be similar. But I have never used it and am not familiar enough with it to know. – space97 Jul 29 '20 at 18:23

0 Answers0