在 cakephp 中为分页 HABTM 逻辑构造传递参数的错误方法?
对于我在这里所做的事情,有几件事我不明白。首先,我不确定我应该如何构建我的控制器逻辑。这是第一部分:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您在该 URL 中没有控制器。通常 Cake 需要这种结构:
因此,在您的场景中,如果您有 RestaurantController,我会将
restaurants
方法重命名为index
(否则您最终会得到重复的restaurants
在 URL 中):编辑:
您的
index
方法应该能够处理不带参数的调用,但目前它无法做到这一点。如果您提供默认值,则当 URL 中未附加任何类型时,将不会显示有关缺少参数的错误。The problem is you don't have a controller in that URL. Normally Cake expects this structure:
So in your scenario, if you have RestaurantController, I would rename the
restaurants
method toindex
(otherwise you'll end up with a duplicaterestaurants
in the URL):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.