英文:
Crud/CakePHP: placing findMethod in the controller
问题
我正在参考这个文档:
https://crud.readthedocs.io/en/latest/actions/index.html#findmethod
有没有办法将查找方法放在控制器中,而不是在App\Model\Table
中?
我想要实现的目标:
我想要在index
操作中应用筛选器,取决于一些条件。这意味着,例如,对于Users
,分页应该只显示名字以特定字母开头的用户,而这个字母将在页面刷新时提供,并且会不断更改。
如果不可能的话,有没有办法在从数据库加载任何数据之前应用此筛选器?
我在UsersController.php
中尝试了这段代码,但表格总是显示所有行。我显然不理解它是如何工作的。
public function index()
{
$users = $this->paginate($this->Users->find()->where(["id" => 1] ));
$this->set(compact("users"));
return $this->Crud->execute();
}
但是,当我测试时,在where
条件中使用一个未知字段[例如bar
]时,我会收到这个 - 正确的 - 错误消息:
Column not found: 1054 Unknown column 'bar' in 'where clause'
所以,显然,where
条件被应用,但由于某种原因,返回了所有行,而不仅仅是id = 1
的1行。我漏掉了什么?
更新:
在查看源代码后,我发现字符串find
被设置为查找方法名称的前缀,因此不可能使用任何其他范围。也许在更新中可以考虑这一点。
所以,现在我有这两种可能性:
- 将其添加到Table文件中。
- 在控制器中设置筛选器,但是我仍然没有弄清楚它是如何工作的。即使我在
$event
中的beforePagination
中设置了它,使用setData("query", $this->Users->find()->where(["id" => 1])
,这仍然被忽略了。
英文:
I am referring to this documentation:
https://crud.readthedocs.io/en/latest/actions/index.html#findmethod
Is there a way to place the find method in the controller, instead in App\Model\Table
?
What I want to achieve:
I want to apply a filter in the index
action, depending on some conditions. This means for example, that the pagination for Users
should show only users whose name begins with a specific letter and this letter will be provided when the page is refreshed and will always change.
If it's not possible, is there a way I can apply this filter before any data is loaded from the database?
I tried this code in UsersController.php
, but the table shows always all rows. I apparently don't understand how it works.
public function index()
{
$users = $this->paginate($this->Users->find()->where(["id" => 1] ));
$this->set(compact("users"));
return $this->Crud->execute();
}
But, when - for testing - I use an unknown field in the where
condition [for example bar
] then I get this - correct - error message:
Column not found: 1054 Unknown column 'bar' in 'where clause'
So, apparently, the where condition is applied but for some reasons all rows are returned instead of only 1 row with id = 1
. What am I missing?
UPDATE:
After having a look into the source code I see that the string find
is set as prefix for the finder method name, so any other scope is not possible. Maybe in an update this could be considered.
So, now I have these 2 possibilites:
- Adding it into the Table file.
- Setting a filter in the
Controller
, but this I still didn't figure out how it works. Even I set it inbeforePagination
in the$event
usingsetData( "query", $this->Users->find()->where( ["id" => 1])
this gets ignored.
答案1
得分: 0
问题 #2 的解决方案:
在 UsersController.php
文件的 index()
方法中添加以下内容:
$this->Crud->on('beforePaginate', function(\Cake\Event\EventInterface $event) {
$this->paginate['conditions']['id'] = <the-id>;
});
英文:
Solution for question #2:
In UsersController.php
in the index()
method add this:
$this->Crud->on('beforePaginate', function(\Cake\Event\EventInterface $event) {
$this->paginate ['conditions']['id'] = <the-id>;
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论