Unused tags are removed on post edit

This commit is contained in:
Marcin Kurczewski 2013-11-01 20:51:19 +01:00
parent 101864459d
commit e1c8139373
2 changed files with 21 additions and 2 deletions

View file

@ -427,6 +427,7 @@ class PostController
} }
R::store($post); R::store($post);
Model_Tag::removeUnused();
$this->context->transport->success = true; $this->context->transport->success = true;

View file

@ -3,7 +3,7 @@ class Model_Tag extends AbstractModel
{ {
public static function locate($key, $throw = true) public static function locate($key, $throw = true)
{ {
$tag = R::findOne('tag', 'LOWER(name) = LOWER(?)', [$key]); $tag = R::findOne(self::getTableName(), 'LOWER(name) = LOWER(?)', [$key]);
if (!$tag) if (!$tag)
{ {
if ($throw) if ($throw)
@ -13,6 +13,24 @@ class Model_Tag extends AbstractModel
return $tag; return $tag;
} }
public static function removeUnused()
{
$dbQuery = R::$f
->begin()
->select('id, name')
->from(self::getTableName())
->where()
->not()->exists()
->open()
->select('1')
->from('post_tag')
->where('post_tag.tag_id = tag.id')
->close();
$rows = $dbQuery->get();
$entities = R::convertToBeans(self::getTableName(), $rows);
R::trashAll($entities);
}
public static function insertOrUpdate($tags) public static function insertOrUpdate($tags)
{ {
$dbTags = []; $dbTags = [];
@ -21,7 +39,7 @@ class Model_Tag extends AbstractModel
$dbTag = self::locate($tag, false); $dbTag = self::locate($tag, false);
if (!$dbTag) if (!$dbTag)
{ {
$dbTag = R::dispense('tag'); $dbTag = R::dispense(self::getTableName());
$dbTag->name = $tag; $dbTag->name = $tag;
R::store($dbTag); R::store($dbTag);
} }