Skip to content
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
Expand Up @@ -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;
Expand All @@ -21,6 +22,7 @@ class TagBehavior extends Behavior
'delimiter' => ',',
'separator' => ':',
'namespace' => null,
'finderField' => 'tag',
'tagsAlias' => 'Tags',
'tagsAssoc' => [
'className' => 'Muffin/Tags.Tags',
Expand All @@ -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'
];
Expand Down Expand Up @@ -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.
Expand Down