0

There are a couple of things I don't understand about what I'm doing here. First, I'm not sure how the heck I should construct my controller logic. Here is the first part:

public function restaurants($restaurantType) {

$this->set('title', $this->params['id'].' restaurants in and near Gulf Shores, AL');
$f=$this->Restaurant->Cuisine->find('all', array(
            'conditions'=>array(
                    'Cuisine.cuisine_type'=>$restaurantType
                    )
            )
    );
$this->set('restaurantType', $f);

}

This obviously doesn't work. I don't know what to call my variable and cake keeps telling me that the argument for restaurants() is missing. All I need is for the url http://www.somesite.com/restaurants/seafood to show me all restaurants with cuisine_type seafood. Instead, it returns nothing. I had this controller logic before:

$this->paginate['Restaurant']['Cuisine']=array(
            'limit'=>9,
            'order' => 'RAND()',
            'contain'=>array(
                    'User'=>array('id'),
                    'Cuisine',
                    'Location',
                    'Image',
                    'Coupon'=>array('promo_code', 'description')
                    ),
            'conditions'=>array(
                    'Cuisine.cuisine_type'=>$this->params['id']
                    'Restaurant.active'=>1

                    )   
        );
    $data = $this->paginate('Restaurant');
    $this->set('restaurantType', $data);

and that gave me EVERYTHING in my database. and still throws the argument missing error.
2nd, this is a HABTM relationship. I read on this Stack Overflow thread that I needed to manually make my joins. So here it is:

 public $paginate=array(
    'joins' => array(
         array( 
           'table' => 'restaurants_cuisines', 
           'alias' => 'RestaurantsCuisine', 
           'type' => 'inner',  
           'conditions'=> array('RestaurantsCuisine.restaurant_id = Restaurant.id') 
       ), 
       array( 
           'table' => 'cuisines', 
           'alias' => 'Cuisine', 
           'type' => 'inner',  
           'conditions'=> array( 
               'Cuisine.id = RestaurantsCuisine.cuisine_id'
               )
           ) 
    )
);

I think this part is right, as I'm not getting anything weird from it. I think I just don't know how to pass a parameter correctly. Any help would be very much appreciated!

UPDATE Here is my new, improved index function:

 public function index($restaurantType) {



$this->paginate['Restaurant']['Cuisine']=array(
        'limit'=>9,
        'order' => 'RAND()',
        'contain'=>array(
                'User'=>array('id'),
                'Cuisine',
                'Location',
                'Image',
                'Coupon'=>array('promo_code', 'description')
                ),
        'conditions'=>array(
                'Cuisine.cuisine_type'=>$this->params['id'],
                'Restaurant.active'=>1

                )   
    );
$data = $this->paginate('Restaurant');
$this->set('restaurantType', $data);
}
Community
  • 1
  • 1
huzzah
  • 1,793
  • 2
  • 22
  • 40

1 Answers1

0

The problem is you don't have a controller in that URL. Normally Cake expects this structure:

http://www.somesite.com/controller_name/action_name/id

So in your scenario, if you have RestaurantController, I would rename the restaurants method to index (otherwise you'll end up with a duplicate restaurants in the URL):

http://www.somesite.com/restaurants/index/seafood

EDIT:

Your index method should be able to handle a call without arguments, a present it isn't able to. If you provide a default the error about missing arguments won't show when no type is appended to the URL.

<?php
  public function index($restaurantType=null) {
    //Rest of your method's code
  }
?>
mensch
  • 4,411
  • 3
  • 28
  • 49
  • Okay, I removed my index() function (which called all of the restaurants up) and renamed the restaurants($restaurantType) to index(). Made sure my route for /restaurants/:restaurantType goes to action index. It still gives me the missing argument error. – huzzah Feb 03 '12 at 14:54
  • I assume `:restaurantType` is a typo and you just mean `restaurantType`? Could you update your question with you `index` method from the RestaurantsController? – mensch Feb 03 '12 at 15:09
  • actually, :restaurantType is what another Stack Overflower told me to write in my routes file. Updating the OP for you with the index method. – huzzah Feb 03 '12 at 15:17
  • I changed my routes back to restaurants/* and the missing argument error goes away, but cake still doesn't pull only records with the correct restaurantType (just pulls all of my restaurants). – huzzah Feb 03 '12 at 15:23
  • I've updated my answer, but it's hard for me to judge if your routing is properly implemented. – mensch Feb 03 '12 at 15:26
  • I had actually tried that already, and the results are the same (no filtering of cuisine type). This is what I have for routes for RestaurantsController-----Router::connect('/restaurants', array('controller'=>'restaurants', 'action' => 'index')); Router::connect('/restaurants/*', array('controller'=>'restaurants', 'action' => 'index')); – huzzah Feb 03 '12 at 16:14
  • Have a look at this section of the manual regarding routing and parameters: http://book.cakephp.org/2.0/en/development/routing.html#passing-parameters-to-action – mensch Feb 03 '12 at 16:29
  • I did what you told me in another controller and it worked this time, so obviously my problem is somewhere else in this one (I'll track it down!) Thank you for your help. – huzzah Feb 07 '12 at 15:29