在 cakephp 中为分页 HABTM 逻辑构造传递参数的错误方法?

发布于 2025-01-02 08:24:51 字数 2912 浏览 1 评论 0原文

对于我在这里所做的事情,有几件事我不明白。首先,我不确定我应该如何构建我的控制器逻辑。这是第一部分:

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

}

这显然行不通。我不知道如何称呼我的变量,并且 cake 不断告诉我餐厅()的参数丢失了。我所需要的只是网址 http://www.somesite.com/restaurants/seafood向我显示所有提供美食类型海鲜的餐厅。相反,它什么也不返回。我之前有这个控制器逻辑:

$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);

这给了我数据库中的一切。并仍然抛出参数丢失错误。
第二,这是一个HABTM关系。我在这个堆栈溢出线程上读到我需要手动进行连接。所以就是这样:

 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'
               )
           ) 
    )
);

我认为这部分是正确的,因为我没有从中得到任何奇怪的东西。我想我只是不知道如何正确传递参数。任何帮助将非常感激!

更新这是我新的、改进的索引函数:

 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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日记撕了你也走了 2025-01-09 08:24:51

问题是您在该 URL 中没有控制器。通常 Cake 需要这种结构:

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

因此,在您的场景中,如果您有 RestaurantController,我会将 restaurants 方法重命名为 index (否则您最终会得到重复的 restaurants 在 URL 中):

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

编辑:

您的 index 方法应该能够处理不带参数的调用,但目前它无法做到这一点。如果您提供默认值,则当 URL 中未附加任何类型时,将不会显示有关缺少参数的错误。

<?php
  public function index($restaurantType=null) {
    //Rest of your method's code
  }
?>

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
  }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文