[英]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.