Joomla 3.1.4:如何以编程方式插入标签


Joomla 3.1.4: how to insert tags programmatically?

在Joomla 3.1.1中,以下是我用来批量插入文章(和标签)的简化代码:

$table = JTable::getInstance('Content', 'JTable', array());
$data = array(
    'title' => $my_title,
    'introtext' => $my_introtext,
    ....
    'metadata' => array(
          ...,
         'tags' => $list_of_tag[id],
          ...,
       ),
  );
$table->bind($data);
$table->check();
$table->store();

然后,$list_of_tag[ids]进入形式为{"tags":[ids],"robots":"","author":"","rights":"","xreference":""}的#_content metadata字段。Joomla还将处理其他相关的表,如#_contentitem_tag_map等。

这种方法在Joomla 3.1.4中不起作用,因为标签不再进入metadata字段,新格式为{"robots":"","author":"","rights":"","xreference":""},即不再有tags键。

有人知道我如何在3.1.4中以编程方式将标签插入Joomla吗?谢谢,

完整代码更新:

3.1.1中的完整代码,其中$row['tags']是一个整数数组,对应于#_tags中的现有标记id,$row中的所有其他字段都定义良好。

<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once (JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_BASE . DS . 'administrator' . DS . 'components' . DS . 'com_content');
$mainframe = JFactory::getApplication('site');
require_once (JPATH_ADMINISTRATOR.'/components/com_content/models/article.php');
$string = file_get_contents("items.json");
$json_str = json_decode($string, true);
$title_default = 'No Title';
$i = 0;
foreach($json_str as $row){
    $table = JTable::getInstance('Content', 'JTable', array());
    $data = array(
        'title' => $row['title'][0],
        'alias' => $row['alias'][0],
        'introtext' => $row['content'],
        'state' => 1,
        'catid' => $row['catid'][0],
        'created' => $row['pdate'],
        'created_by' => 635,
        'created_by_alias' => $row['poster'][0],
        'publish_up' => $row['pdate'],
        'urls' => json_encode($row['urls']),
        'access' => 1,
        'metadata' => array(
            'tags' => $row['tags'],
            'robots' => "",
            'author' => implode(" ", $row['poster']),
            'rights' => "",
            'xreference' => "",
        ),
    );
    ++$i;
// Bind data
    if (!$table->bind($data))
        {
            $this->setError($table->getError());
            return false;
        }
// Check the data.
    if (!$table->check())
        {
            $this->setError($table->getError());
            return false;
        }
// Store the data.
    if (!$table->store())
        {
            var_dump($this);
            $this->setError($table->getError());
            return false;
        }
    echo 'Record ' . $i . ' for post ' . $data['alias'] . ' processed';
    echo "'r'n";
}
?>

在阅读文档时,我尝试了不同的方法来重写代码:

  1. 将元数据下表示"tags"=>$row['tags']的行移动到其父数组,即:

        ...
        'access' => 1,
        'tags' => $row['tags'],
        'metadata' => array(
            'robots' => "",
            'author' => implode(" ", $row['poster']),
            'rights' => "",
            'xreference' => "",
        ),
        ...
    

因此,现在我们有$data['tags'],其中填充了一个映射现有标记id的整数数组,假设已经为JTablestore()方法做好了准备;

  1. 除了方法1之外,还可以使用jsonify$row['tags']。为此,我尝试了两种方法:

2.a)

...
$registry = new JRegistry();
$registry->loadArray($row['tags']);
$data['tags'] = (string) $registry;
...

2.b)

data['tags'] = json_encode(json_encode($row['tags']));

有了这些更改,我仍然无法为插入的文章添加标签。

艾琳:谢谢你的耐心!

http://docs.joomla.org/J3.1:Using_Tags_in_an_Extension是在扩展中使用标记的基本文档。

尽管3.1.4中有变化,但如果您遵循这些说明,它将起作用。3.1.4+使它变得更容易,因为它通过观察者模式来处理标签。我将尝试更新文档,但您可以查看任何核心组件,并看到代码已经简化,并在一定程度上脱离了JTable。

更新:

我更新了3.1.4的文档,包括如何修改旧代码使其工作。

事实证明,如果您实例化JTable,那么新的com_tag将不会使用$data['tags'],相反,您需要将您的标签直接绑定到$table作为table->newTags = $data['tags'];,这样,您新插入的文章将被正确地标记,因为您已经用现有的标签ID填充了$data['tags']。

答案很晚,但希望能将下一个OP从我试图直接调用Tag方法的挫折中拯救出来。

我最终只是简单地更新了文章,用内容模型标记:

$basePath = JPATH_ADMINISTRATOR.'/components/com_content';
require_once $basePath.'/models/article.php';
$articlemodel = new ContentModelArticle(array('table_path' => $basePath . '/tables'));
$params = array(
    'id' => 123,                // Article being tagged
    'tags' => array(7,8,9,14)   // Tag IDs from #__tags to tag article with
);
if($articlemodel->save($params)){
    echo 'Success!';
}

工作起来很有魅力!它似乎可以很容易地适应任何可标记的项目。我想我的情况与最初的问题类似,实际上我使用了上面的代码和SQL语句,为我编译了正确的标签ID。这对答案来说毫无意义,但它使我不得不手动标记从200多个标签中选择的1900篇文章!!