【问题标题】:Joomla 3.3 Programmatically adding tag to articleJoomla 3.3 以编程方式向文章添加标签
【发布时间】:2014-12-03 07:58:00
【问题描述】:

我正在尝试为文章添加标签 (Joomla 3.3.1)

function saveTags(JTableInterface $table, $newTags = array(), $replace = true){
  $th = new JHelperTags();
  $data = $th->getRowData($table);
  $ucmContentTable = JTable::getInstance('Corecontent');
  $ucm = new JUcmContent($table, 'com_content.article');
  $ucmData = $data ? $ucm->mapData($data) : $ucm->ucmData;
  $primaryId = $ucm->getPrimaryKey($ucmData['common']['core_type_id'], $ucmData['common']'core_content_item_id']);
  $result = $ucmContentTable->load($primaryId);
  $result = $result && $ucmContentTable->bind($ucmData['common']);
  $result = $result && $ucmContentTable->check();
  $result = $result && $ucmContentTable->store();
  $ucmId = $ucmContentTable->core_content_id;

$result = $result && $th->tagItem($ucmId, $table, $newTags, $replace);
}

$table = JTable::getInstance('Content', 'JTable', array());
$table->bind($dataContent);
$table->check();
$table->store();
saveTags($table, array("tag_one", "tag_two"));

但是有一个错误—— 代码:

Duplicate entry '0-281-0' for key 'uc_ItemnameTagid' SQL=INSERT INTO new1_contentitem_tag_map
(`type_alias`,`core_content_id`,`content_item_id`,`tag_id`,`tag_date`,`type_id`) VALUES 
('', 12, 281, '0', CURRENT_TIMESTAMP(), 0),('', 12, 281, '0', CURRENT_TIMESTAMP(), 0)

我的代码有什么问题?

【问题讨论】:

    标签: joomla3.0


    【解决方案1】:

    不要直接拨打tagItem。使用观察者。

    $table = JTable::getInstance('Content', 'JTable', array());
    $table->bind($data);
    $table->check();
    $table->store();
    //you need observer
    $tagsObserver = $table->getObserverOfClass('JTableObserverTags');
    $tagsObserver->onBeforeStore(true, false);//arguments does not matter here
    $tagsObserver->setNewTags(array("1","2","3"), true); //array of tag IDs
    

    【讨论】:

      猜你喜欢
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 2015-09-17
      • 2011-07-07
      • 1970-01-01
      相关资源
      最近更新 更多