[英]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
字段{"tags":[ids],"robots":"","author":"","rights":"","xreference":""}
。 Joomla还会处理其他相关表,例如#_contentitem_tag_map
等。
此方法在Joomla 3.1.4中不起作用,因为标签不再进入metadata
字段,新格式为{"robots":"","author":"","rights":"","xreference":""}
,即没有更多tags
键。
有谁知道如何在3.1.4中以编程方式将标签插入Joomla? 谢谢,
更新完整代码:
在3.1.1中工作的完整代码,其中$ row ['tags']是一个整数数组,对应于#_tags中的exing标记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";
}
?>
在阅读文档时,我尝试了不同的方法来重写代码:
将元数据下的'tags'=> $ row ['tags']的行移动到其父数组,即:
... 'access' => 1, 'tags' => $row['tags'], 'metadata' => array( 'robots' => "", 'author' => implode(" ", $row['poster']), 'rights' => "", 'xreference' => "", ), ...
所以现在我们有$ data ['tags']填充了一个映射现有标记id的整数数组,预先准备好了JTable store()方法;
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_tags将不会采用$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!';
}
像魅力一样工作! 似乎它可以很容易地适应任何可标记的项目。 我认为我遇到了与原始问题类似的情况,实际上使用了上面的代码和一个为我编译正确标签ID的SQL语句。 这对答案毫无意义,但它让我不得不手动标记来自200多个标签的1,900篇文章!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.