3

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.

  1. 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)

  2. 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.

  3. 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?

laketuna
  • 3,832
  • 14
  • 59
  • 104

3 Answers3

1

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()

Jack
  • 5,680
  • 10
  • 49
  • 74
  • Could you elaborate on the reasoning behind another controller? From what I've been reading, this approach makes more sense to me, but I can't exactly explain why it is in my current state of knowledge. – laketuna Apr 17 '12 at 23:45
  • You want to separate it into another controller so you can unify your searching process. Easier to manage if you decide to add other models like search "customers" or something. And I'd rather have the URLs start with search and be more consistent. – Jack Apr 18 '12 at 01:04
1

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.

Dave
  • 28,833
  • 23
  • 113
  • 183
  • When you say "don't use `Pages` controller," are you saying that I should go to `Searches` controller directly when a user clicks on "Search" on the menu? So, when "Search" is clicked, URL goes to `myapp/searches/products`, bypassing `Pages` controller completely. – laketuna Apr 18 '12 at 18:46
  • Correct. The Pages controller is for static content (an About Us page for example). – Dave Apr 18 '12 at 18:49
  • OK, so I got this completed and I'm having the search result display below the search form (`views/searches/products.ctp`), but I don't think I can naturally use `Paginator` here. `$this->find()` is being done in the model here, but `$this->paginate()` needs to be done in a controller, and my controllers don't have these search logics. How do I go about paginating my search result in a view while having the search logic in the model? – laketuna Apr 18 '12 at 19:39
  • Post this as another question, and I'd be glad to answer it (it's too much to answer in a comment, but I am doing just that in my project, so - don't fret - it works. :) (post the link to your new question here, and I'll answer it) – Dave Apr 18 '12 at 19:50
  • Superb Dave! Here: http://stackoverflow.com/questions/10217087/cakephp-paginating-with-search-logic-in-a-model – laketuna Apr 18 '12 at 20:04
0
  1. You don't need to create a new Controller for that. You can have the same controller (saying Products) and an action called Search on that controller. So your link will point to /Products/Search
  2. Even creating a new controller, you can access other models by importing their corresponding controller
  3. I'll suggest stick on what Cake usually do, create a Controller for each Model that you have on your application. Normaly each Model should be a class that represents an entity (physical or logical) on the scenario that you're translating into your system and you'll have a Controller for each of those methods.
pollirrata
  • 5,188
  • 2
  • 32
  • 50
  • So, if I take your approach, we're going between `myapp/product/search` and `myapp/order/search` within this search page, depending what I search for. This is fine? – laketuna Apr 17 '12 at 22:49
  • yeah, that will be the approach described above – pollirrata Apr 17 '12 at 22:57