【问题标题】:Cakephp 3: How to ignore beforefind for specific queries?Cakephp 3:如何忽略特定查询的先行查找?
【发布时间】:2015-01-09 12:23:01
【问题描述】:

我正在处理多语言帖子。我在 PostsTable 中添加了 beforefind(),这样我就可以列出当前语言的帖子

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

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

为了让用户可以用不同的语言复制帖子,我编写了以下函数:

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]);
}

为了检查帖子是否已经重复。我正在检查“编辑”功能:

    $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() 。我尝试使用如下实体:

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

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

但没有运气。有人可以指导我吗?谢谢阅读。

【问题讨论】:

    标签: entity exists cakephp-3.0


    【解决方案1】:

    有多种可能的方法来处理这个问题,一种是利用Query::applyOptions() 设置一个选项,您可以在回调中检查

    $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()]);
        }
    }
    

    警告$options 参数当前作为数组传递,而它应该是 ArrayObject (#5621) 的实例

    【讨论】:

    • 谢谢@ndm 这很有帮助
    • 正是我需要的,以防止在某些情况下强制条件。谢谢!
    【解决方案2】:

    Callback methods 可以这样忽略:

    $this->Model->find('all', array(
         'conditions' => array(...),
         'order' => array(...),
         'callbacks' => false
    ));
    

    【讨论】:

    • 谢谢 felix 先生 :) 我会试试这个。
    猜你喜欢
    • 2014-11-29
    • 2013-08-17
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    相关资源
    最近更新 更多