Cakephp 3:如何忽略特定查询的事前发现? [英] Cakephp 3: How to ignore beforefind for specific queries?

查看:53
本文介绍了Cakephp 3:如何忽略特定查询的事前发现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事多语种职位。我在PostsTable中添加了beforefind(),以便可以列出当前语言的帖子

I am working on multilingual posts. I have added beforefind() in the PostsTable so I can list posts for current language

public function beforeFind(Event $event, Query $query) {

    $query->where(['Posts.locale' => I18n::locale()]);
}

为了允许用户重复使用不同语言的帖子,我写了以下函数: / p>

In order to allow users duplicate posts in different languages i wrote following function:

public function duplicate(){
    $this->autoRender = false;
    $post_id= $this->request->data['post_id'];

    $post = $this->Posts
            ->findById($post_id)
            ->select(['website_id', 'category_id', 'locale', 'title', 'slug', 'body', 'image', 'thumb', 'meta_title', 'meta_description', 'other_meta_tags', 'status'])
            ->first()
            ->toArray();

    foreach($this->request->data['site'] as $site) {
        if($site['name'] == false) {
            continue;
        }
        $data = array_merge($post, [
            'website_id' => $site['website_id'],
            'locale' => $site['locale'],
            'status' => 'Draft',
            'duplicate' => true
        ]);


        $pageData = $this->Posts->newEntity($data);

        if($this->Posts->save($pageData)) {
            $this->Flash->success(__('Post have been created.'));;
        } else{
            $this->Flash->error(__('Post is not created.'));
        }

    }

    return $this->redirect(['action' => 'edit', $post_id]);
}

为了检查帖子是否已重复。我正在'edit'functino中进行检查:

In order to check if the posts are already duplicated. I am doing a check in 'edit' functino:

    $languages = TableRegistry::get('Websites')->find('languages');

    foreach($languages as $language)
    {
        $exists[] = $this->Posts
                    ->findByTitleAndWebsiteId($post['title'], $language['website_id'])
                    ->select(['locale', 'title', 'website_id'])
                    ->first();
    }
    $this->set('exists',$exists); 

,但是由于beforefind()将查询追加到上述查询中。我没有任何结果。有什么方法可以只对某些查询忽略beforefind()。我尝试使用实体,如下所示:

but as the beforefind() is appending query to above query. I am not getting any results. Is there any way I can ignore beforefind() for only cerrtain queries. I tried using entity as below:

public function beforeFind(Event $event, Query $query) {

    if(isset($entity->duplicate)) {
        return true;
    }
    $query->where(['Posts.locale' => I18n::locale()]);
}

但没有运气。谁能指导我?谢谢阅读。

but no luck. Could anyone guide me? Thanks for reading.

推荐答案

有多种方法可以解决此问题,一种方法是利用 Query :: applyOptions()设置一个可以在回调中签入的选项

There are various possible ways to handle this, one would be to make use of Query::applyOptions() to set an option that you can check in your callback

$query->applyOptions(['injectLocale' => false])



public function beforeFind(Event $event, Query $query, ArrayObject $options)
{
    if(!isset($options['injectLocale']) || $options['injectLocale'] !== false) {
        $query->where(['Posts.locale' => I18n::locale()]);
    }
}

警告: code> $ options 参数当前作为数组传递,而它应该是 ArrayObject #5621

Warning: The $options argument is currently passed as an array, while it should be an instance of ArrayObject (#5621)

这篇关于Cakephp 3:如何忽略特定查询的事前发现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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