Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Joomla 3.1.4:如何以编程方式插入标记?_Php_Joomla - Fatal编程技术网

Php Joomla 3.1.4:如何以编程方式插入标记?

Php Joomla 3.1.4:如何以编程方式插入标记?,php,joomla,Php,Joomla,在Joomla 3.1.1中,以下是我用来批量插入文章(和标签)的简化代码: 标签[ID]的$list_然后进入#_内容元数据字段,格式为{“标签”:[ID],“机器人”:“作者”:“权限”:“外部参照”:“}。Joomla还将处理其他相关的表,如##u contentitem_tag_map等 此方法在Joomla 3.1.4中不起作用,因为标记不再进入元数据字段,新格式为{“robots”:““author”:“、”rights”:““xreference”:“},即不再有标记键 有人知道如

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

标签[ID]的
$list_然后进入#_内容
元数据
字段,格式为
{“标签”:[ID],“机器人”:“作者”:“权限”:“外部参照”:“}
。Joomla还将处理其他相关的表,如
##u contentitem_tag_map

此方法在Joomla 3.1.4中不起作用,因为标记不再进入
元数据
字段,新格式为
{“robots”:““author”:“、”rights”:““xreference”:“}
,即不再有
标记

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

完整代码的更新:

在3.1.1中使用的完整代码,$row['tags']是一个整数数组,对应于#U标记中的现有标记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";
}
?>
现在我们有了$data['tags'],其中填充了一个映射现有标记ID的整数数组,可能已经为JTable store()方法做好了准备

  • 除了方法1之外,jsonify$row['tags']。为此,我尝试了两种方法:
  • 2.a)

    2.b)

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

    埃琳:谢谢你的耐心

    是在扩展中使用标记的基本文档

    尽管3.1.4+中有更改,但如果您按照这些说明操作,它将起作用。3.1.4+使其变得更容易,因为它通过观察者模式处理标记。我将尝试更新文档,但您可以查看任何核心组件,并看到代码已经简化,并在某种程度上移出了JTable

    更新:


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

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

    延迟回答,但希望能避免我在试图找到直接调用标记方法的方法时遇到的挫折

    最后,我只是更新了要用内容模型标记的文章:

    $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篇文章

    你的意思是把它们放在一个要展示的项目中吗?最简单的方法是在视图布局中嵌入标记JLayout。标记永远不应该进入您的内容表。。数据仅记录在内容映射表中。tagItem()是从JhelpartAgsTagItem()传递的方法,需要传递$ucmId,如何在代码中返回此变量?谢谢,当api进行保存时会发生这种情况,这就是为什么要进行预处理/后处理。我在3.1.1中使用了相同的代码,它负责ucm表、标记映射等,它只是停止了那样的工作。问题是文档向您展示了如何添加标记,但您正在以非标准方式对其进行编码。3.1.1中的变更并未影响任何遵守本文件的人员。总的来说,你当然应该能够以非标准的方式做事。要获取ucmId,需要获取ucm_内容表中与要标记的数据相对应的行的id。您可以从ucm_内容或urm_内容库中获得此信息,但请注意,在本文中,值仅使用postStoreProcessing创建ucm_内容行。3.1.5中也存在一些与CLIs和JTable相关的问题,添加新标记如何?
        ...
        'access' => 1,
        'tags' => $row['tags'],
        'metadata' => array(
            'robots' => "",
            'author' => implode(" ", $row['poster']),
            'rights' => "",
            'xreference' => "",
        ),
        ...
    
    ...
    $registry = new JRegistry();
    $registry->loadArray($row['tags']);
    $data['tags'] = (string) $registry;
    ...
    
    data['tags'] = json_encode(json_encode($row['tags']));
    
    $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!';
    }