phpentitycakephp-3.0exists

Cakephp 3: How to ignore beforefind for specific queries?


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

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

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

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.


Solution

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

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