Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #69 from okinaka/saving-with-identifier
Browse files Browse the repository at this point in the history
Saving with identifier
  • Loading branch information
Florian Krämer committed Apr 21, 2014
2 parents 2bba473 + b378ddd commit a806609
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Model/Behavior/TaggableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,15 @@ public function saveTags(Model $model, $string = null, $foreignKey = null, $upda
extract($this->disassembleTags($model, $string, $this->settings[$model->alias]['separator']));

if (!empty($tags)) {
$conditions = array('OR' => array_map(function ($tag) use ($tagModel) {
return array(
$tagModel->alias . '.identifier' => $tag['identifier'],
$tagModel->alias . '.keyname' => $tag['keyname'],
);
}, $tags));
$existingTags = $tagModel->find('all', array(
'contain' => array(),
'conditions' => array(
$tagModel->alias . '.keyname' => Set::extract($tags, '{n}.keyname')),
'conditions' => $conditions,
'fields' => array(
$tagModel->alias . '.identifier',
$tagModel->alias . '.keyname',
Expand Down
15 changes: 14 additions & 1 deletion Test/Case/Model/Behavior/TaggableBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function testTagArrayToString() {
$this->assertTrue(empty($result));
$this->assertInternalType('string', $result);

$data['tags'] = 'cakephp:foo, cakephp:bar';
$data['tags'] = 'cakephp:foo, cakephp:bar, foo, bar';
$this->Article->save($data, false);
$result = $this->Article->find('first', array(
'conditions' => array(
Expand Down Expand Up @@ -373,4 +373,17 @@ public function testSavingTagsDoesNotCreateEmptyRecords() {
$this->assertEquals($count, 0);
}

/**
* testSavingTagsWithDefferentIdentifier
*
* @return void
*/
public function testSavingTagsWithDifferentIdentifier() {
$data = $this->Article->findById('article-1');
$data['Article']['tags'] = 'foo:cakephp, bar:cakephp';
$this->Article->save($data);
$data = $this->Article->findById('article-1');
$this->assertEquals('bar:cakephp, foo:cakephp', $data['Article']['tags']);

}
}

0 comments on commit a806609

Please sign in to comment.