Reputation: 4080
I have a simple web application with database tables orders
and products
that I'm reworking with CakePHP. There are more, but for simplicity, I'm using just these two here. In the web app, I'd like to be able to search items, search orders, and do order-related jobs, but I'm having difficulty setting up and choosing components to use to make this web app. As an example to my problem, I have the following page. Check out the layout below.
|------------------------------|
| My App |
|------------------------------|
|Search <--| search order |
|Orders | search product |
|etc. | |
| | |
| | |
|------------------------------|
Figure: left navigation bar and body shown. Arrow denotes page we're on.
Controllers: for this search page, which controllers should be used? I'm defaulting to PagesController
for static pages right now, but should this search page really use PagesController
? I also have ProductsController
and OrdersController
that were created when I created Product
and Order
models, but it seems more appropriate to use a completely new controller, perhaps one called SearchController
. This way the URL stays under myapp/search/...
and doesn't go all over the place. Is this the proper way? (I know I can rewrite URLs, but that's for later)
Models and actions: I have Product
and Order
models. Each of these models have a search logic for its respective DB table. If a new controller was created for the above page, then that controller can simply use these models' actions right? I'm reading controller and model separation is completely normal and actually a good thing. At first I thought they went together, but the more I familiarize with CakePHP, it seems to be the opposite.
If my approach (from 1 and 2) is not inefficient, wrong, or improper, could you suggest a good structure to set this specific page up with respect to what controllers to use and how the controller should be using models and their actions?
Upvotes: 3
Views: 411
Reputation: 29121
TLDR:
The real answer is - personal preference. It's not a big deal either way whether you create a SearchesController
or use the ProductsController
and OrdersController
(but don't use PagesController).
My preference and reasoning
I would create a SearchesController
. This just makes the most sense for organizational purposes. All your search functionality can be in the same controller, all your search views will be in the "Views/Searches/" folder, and any/all related css and javascript can be in /css/searches
or /js/searches/
(if you like to organize that way like I do).
You'll also want to make a Search model (Models/Search.php
) and within it, add: public $useTable = false;
[details] to tell it you aren't going to make a searches
table.
Within each of the SearchController's actions like function orders()
and function products()
, you can use $this->loadModel('Order');
[details], to make those models accessible from the SearchesController
, then run your searches.
To stick with the "Fat Models, Skinny Controllers" mantra, I recommend keeping your controller code small - something like this:
$opts = array('limit'=>5);
$orders = $this->Order->search($string, $opts);
Then, in your Order model (per this example), do the meat of the logic (below is rough idea):
public function search($string = null, $opts = null) {
$params = array();
$params['limit'] = 10; //sets default
if(!empty($opts['limit'])) $params['limit'] = $opts['limit'];
//... build options, contains, limits...etc
return $this->find('all', $params);
}
Keeping all your controller actions together allows you to more easily make sweeping changes - like if you want to set a single variable for results per page that changes all your search pages...etc. I'm sure there are a lot of other ideas, but bottom line, it's keeping similar code together.
Upvotes: 1
Reputation: 5768
I suggest you use another search controller. Inside the search controller, at the class level just add this line var $uses = array('Order', 'Product');
so you can access the Order and Product tables.
Simply use $this->Order->find()
and $this->Product->find()
Upvotes: 1
Reputation: 5286
Upvotes: 0