与关联定义匹配的自定义查找器? [英] custom finder with matching in association definition?

查看:60
本文介绍了与关联定义匹配的自定义查找器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型:Contacts和Groups具有groupsToMany关联。



我只想获取该联系人可以访问的组。为此,我有一个自定义查找器。

 公共函数findAccessible(Query $ query,array $ options){
return $ query
->其中(['admin_user_id'=> $ options ['User.id']])
-> orWhere(['public'=> true])
-> matching('Users',function($ q)use($ options){
return $ q-∨ Where(['Users.id'=> $ options ['User.id ']]);
});
}

所以我可以打电话给关注者,我会得到想要的东西。

  $ accessibleGroups = $ this-> Contacts-> Groups-> find('accessible',['User.id' => $ this-> Auth-> user('id')]); 

但是,如果我有一个容器,它将使所有组都归还给所有可访问的组。

  $ contact = $ this-> Contacts-> get($ id,[
'contain'=> ['Groups']
]);

如何限制包含对象的可访问性?



<我无法将自定义查找器添加到表关联定义的finder属性中,因为我无法在此处传递$ options。或者我可以吗?

解决方案

让我引用文档和测试(如果您在文档中找不到某些内容,测试通常是有关操作方法的有用信息。)



http://book.cakephp.org/3.0/en/orm/table-objects.html#passing-conditions-包含


如果在关联表中定义了一些自定义查找器方法,则可以使用它们里面包含:

  //携带所有文章,但仅携带批准的评论和
//受欢迎。
$ query = $ articles-> find()-> contain([
'Comments'=> function($ q){
return $ q-> find('已批准')->查找('受欢迎');
}
]);


在这种情况下,您可以简单地输入 find()调用,就像您已经在做一样。






http://book.cakephp .org / 3.0 / en / orm / table-objects.html#using-the-finder-option



https://github.com/cakephp/cakephp/blob /7fc4cfe3ae7d4c523331a44e2862bab5c8f44f1e/tests/TestCase/ORM/QueryTest.php#L2175



所以还有这个隐藏 查找器选项,可以代替可调用的选项使用:

  $ table-> find('all')
-> where([[Articles.author_id'=> $ authorId])
-> conta in([[
'Authors'=> [
finder => [’byAuthor’=> [’author_id’=> $ authorId]]
]
]);






我想如果发现者不会受到伤害用法将在Cookbook中详细记录, Query :: contain()的文档块也缺少有关此信息。


I have two models: Contacts and Groups with belongsToMany association.

I want to get only that groups what is accessible for the contact. For this I have a custom finder.

public function findAccessible(Query $query, array $options){
    return $query
            ->where(['admin_user_id' => $options['User.id']])
            ->orWhere(['public' => true])
            ->matching('Users', function($q) use ($options){
                return $q->orWhere(['Users.id' => $options['User.id']]);
            });
}

So I can call to following and I will get what I want.

$accessibleGroups = $this->Contacts->Groups->find('accessible', ['User.id' => $this->Auth->user('id')]);

But if I have a contain, than it will give back all groups not just accessible ones.

$contact = $this->Contacts->get($id, [
        'contain' => ['Groups']
    ]);

How to limit the contain to accessible?

I can not add the custom finder to the table association definition's finder property, as I can not pass the $options there. Or can I?

解决方案

Let me quote the docs and the tests (if you can't find something in the docs, the tests are often a useful source for information on how to do things).

http://book.cakephp.org/3.0/en/orm/table-objects.html#passing-conditions-to-contain

If you have defined some custom finder methods in your associated table, you can use them inside contain:

// Bring all articles, but only bring the comments that are approved and
// popular.
$query = $articles->find()->contain([
    'Comments' => function ($q) {
       return $q->find('approved')->find('popular');
    }
]);

In that scenario you could simply pass in the conditions in the find() call just like you are already doing.


http://book.cakephp.org/3.0/en/orm/table-objects.html#using-the-finder-option

https://github.com/cakephp/cakephp/blob/7fc4cfe3ae7d4c523331a44e2862bab5c8f44f1e/tests/TestCase/ORM/QueryTest.php#L2175

So there's also this "hidden" finder option that can be used instead of a callable:

$table->find('all')
    ->where(['Articles.author_id' => $authorId])
    ->contain([
        'Authors' => [
            'finder' => ['byAuthor' => ['author_id' => $authorId]]
        ]
    ]);


I guess it wouldn't hurt if the finder usage would be documented a little more detailed in the Cookbook, the docblock for Query::contain() is missing info about it too.

这篇关于与关联定义匹配的自定义查找器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆