Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finder method solution #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/Model/Behavior/TagBehavior.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
@@ -21,6 +22,7 @@ class TagBehavior extends Behavior
'delimiter' => ',',
'separator' => ':',
'namespace' => null,
'finderField' => 'tag',
'tagsAlias' => 'Tags',
'tagsAssoc' => [
'className' => 'Muffin/Tags.Tags',
@@ -37,11 +39,14 @@ class TagBehavior extends Behavior
'taggedCounter' => ['tag_count' => [
'conditions' => []
]],
'implementedFinders' => [
'tagged' => 'findByTag'
],
'implementedEvents' => [
'Model.beforeMarshal' => 'beforeMarshal',
'Model.beforeMarshal' => 'beforeMarshal'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the commas please.

],
'implementedMethods' => [
'normalizeTags' => 'normalizeTags',
'normalizeTags' => 'normalizeTags'
],
'fkTableField' => 'fk_table'
];
@@ -87,6 +92,29 @@ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $opti
}
}

/**
* Finder method
* usage $query->find('tagged', ['{finderField}' => 'example_tag' ] );
*
* @param Query $query
* @param array $options
*/
public function findByTag(Query $query, array $options){

if(!empty($options[$this->config('finderField')])){
$query->matching($this->config('tagsAlias'), function ($q) use ($options) {
return $q->where([
'OR' => [
$this->config('tagsAlias').'.slug' => $options[$this->config('finderField')],
$this->config('tagsAlias').'.label' => $options[$this->config('finderField')]
]
]
);
});
}

}

/**
* Binds all required associations if an association of the same name has
* not already been configured.