CakePHP - 构建搜索 - 如何在查询中包含 URL 变量
我正在构建一个搜索页面,该页面将具有用户可以单击的许多不同的搜索参数,这将传递 URL 中的变量。
$this->Restaurant->recursive = 1;
$this->paginate = array(
'conditions' => array(
'City.name' => $this->params['url']['city'],
'Cuisine.name' => $this->params['url']['cuisine'],
),
);
$data = $this->paginate('Restaurant');
$this->set('data', $data);
如果 URL 中有 ?city=newyork&cuisine=pizza ,则效果很好 - 但如果没有,则会出错* 如果是普通 PHP,我会将查询构建为字符串,并仅在变量存在时附加条件存在...等等。但对于 Cake,我不知道从哪里开始,也不知道管理这个问题的最佳方法是什么。
编辑:
*如果我没有将“城市”或“美食”作为 URL 变量传递,我会得到:
<b>Notice</b> (8)</a>: Undefined index: city [<b>APP/controllers
/restaurants_controller.php</b>, line <b>18</b>
...
City.name' => $this->params['url']['city'],
</span></code></span></pre></div><pre class="stack-trace">
RestaurantsController::search() - APP/controllers/restaurants_controller.php
, line 18
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83
如果我只有“城市”和“美食”,那就不会有问题- 但如果我计划以同样的方式传递 20 个以上的搜索选项,我希望能够自行决定传递或不传递它们,而不是每次都被迫将它们全部包含在 URL 中。
I'm building a search page that will have many different search parameters that the user can click on, which will pass the variables in the URL.
$this->Restaurant->recursive = 1;
$this->paginate = array(
'conditions' => array(
'City.name' => $this->params['url']['city'],
'Cuisine.name' => $this->params['url']['cuisine'],
),
);
$data = $this->paginate('Restaurant');
$this->set('data', $data);
This works just fine IF there is ?city=newyork&cuisine=pizza in the URL - but if not, it errors out* If it were normal PHP, I'd build out the query as a string and append conditions only if the variables existed...etc. But with Cake I'm not sure where to begin or what would be the best way to manage this.
EDIT:
*If I do not have 'city' or 'cuisine' being passed as URL variables, I get this:
<b>Notice</b> (8)</a>: Undefined index: city [<b>APP/controllers
/restaurants_controller.php</b>, line <b>18</b>
...
City.name' => $this->params['url']['city'],
</span></code></span></pre></div><pre class="stack-trace">
RestaurantsController::search() - APP/controllers/restaurants_controller.php
, line 18
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83
It wouldn't be a problem if I just had "city" and "cuisine" - but if I plan on having 20 more search options to be passed in the same way, I'd like to be able to pass them or not at my discretion instead of being forced to have them ALL in the URL every time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误
是什么意思?通常在视图中的分页调用之前添加
$this->Paginator->options(array('url' => $this->passedArgs));
视图 将使 cake 将所有传递的 URL 参数添加到它生成的 url编辑
您正在寻找的是命名参数:http://book.cakephp.org/view/947/Named-parameters
如果您看最后一个示例,您将看到路由映射到
ContentsController->view();
,其中包含您(可选)选择在passedArgs数组。
编辑2
您的问题是您有一组可变的搜索条件。您的系统基本上应该遍历搜索条件并根据选择的过滤器构建搜索查询。有几种方法可以解决这个问题。最简单的方法是循环遍历它们。
这样,您就可以将一组可以应用的过滤器列入白名单。然后循环遍历参数并构建
conditions
数组。如果您需要 OR 条件或不同的 MYSQL,则必须对此进行一些修改。但这将允许您拥有可变数量的过滤器。What do you mean
errors out
?Usually adding
$this->Paginator->options(array('url' => $this->passedArgs));
before your pagination call in the view will make cake add all the passed URL params to the url that it generatesEDIT
What you're looking for are named parameters : http://book.cakephp.org/view/947/Named-parameters
If you look at the last example, you'll see that the route maps to
ContentsController->view();
with all the parameters you (optionally) chose to send available in thepassedArgs
array.EDIT 2
Your problem here is that you have a variable set of search criteria. Your system should basically go through the search criteria and build your search query depending what filters have been chosen. There are several ways you can solve this. The easiest would be to loop through them.
This way, you're whitelisting a set of filters that can be applied. And you loop through the params and build your
conditions
array. You'll have to modify this a bit if you need OR conditions or different MYSQL. But this will allow you to have a variable number of filters.cake 使用 MVC 模式,意味着 url 像 http://localhost/controller/action/param1/param2
转换为 index.php?controller=controller&action=action¶m1=param1 ...
如果无法从原始 url 获取查询字符串 (http://localhost/controller.../?a=3& b=2)
您应该将 QSA 参数添加到您的 .htaccess 中,它应该看起来像这样
,然后您将在操作方法中获取查询字符串。
如果您不想使用这种方式,您可以通过控制器的操作获取参数,使用
代替查询字符串,
它看起来像这样
cake use MVC pattern, mean that url like http://localhost/controller/action/param1/param2
convert to index.php?controller=controller&action=action¶m1=pararm1 ...
if you can't get the query string from the original url (http://localhost/controller.../?a=3&b=2)
you should add QSA param to your .htaccess, it should look something like that
and then you will get the query string to your action method.
if you don't want to use this way, you can get the params via the controller's action using
in place of the query string
it will look something like that