Crud/CakePHP:将findMethod放置在控制器中

huangapple go评论59阅读模式
英文:

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被设置为查找方法名称的前缀,因此不可能使用任何其他范围。也许在更新中可以考虑这一点。

所以,现在我有这两种可能性:

  1. 将其添加到Table文件中。
  2. 在控制器中设置筛选器,但是我仍然没有弄清楚它是如何工作的。即使我在$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:

  1. Adding it into the Table file.
  2. Setting a filter in the Controller, but this I still didn't figure out how it works. Even I set it in beforePagination in the $event using setData( "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-&gt;Crud-&gt;on(&#39;beforePaginate&#39;, function(\Cake\Event\EventInterface $event) {
   $this-&gt;paginate [&#39;conditions&#39;][&#39;id&#39;] = &lt;the-id&gt;;
});

huangapple
  • 本文由 发表于 2023年6月2日 05:37:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定