如何使用自定义插件向Joomla 2.5 DB插入值

时间:2013-10-24 17:38:54

标签: database plugins joomla insert components

我正在尝试创建一个joomla插件,它将一个值插入到Joomla组件的数据库表中。我已经通过Joomla Extension Manager成功构建并安装了插件,但是当我启用插件时,我没有看到数据库表中插入的值。以下是我要查看的插件的php代码:

<?php
######################################################################
# Outkast Plugin                                                     #
######################################################################

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin');
jimport( 'joomla.html.parameter');

class plgSystemOutkastplugin extends JPlugin
{
function plgSystemOutkastplugin()
{

$db = JFactory::getDbo();
$name = "http://www.youtube.com/embed/PWgvGjAhvIw?rel=0";

$columns = array('title', 'link_youtube');
$name = "http://www.youtube.com/embed/PWgvGjAhvIw?rel=0";    
$values = array($db->quote('Outkast'), $db->quote($name));

$query->insert($db->quoteName('#__mycomponent_outkastvideos'))
  ->columns($db->quoteName($columns))
  ->values(implode(',', $values));

$db->setQuery($query);
$db->query();

return true;

}        

}
?>

编辑更新11/07/13: 我正在添加插件中的xml文件,以帮助提供更多问题的上下文。最后我有上面的php文件和下面的xml的组合来创建整个插件包。当我在Joomla 2.5站点上安装组件时,它安装没有错误。然后,当我启用插件时,我得到两个插入数据库的视频实例。当我禁用插件时,我将获得一个插入数据库的视频实例。这不是我追求的。

理想情况下,当用户安装插件时,我希望它能自动启用该插件。如果启用了插件,则视频将显示在数据库中。如果禁用该插件,则视频将不会显示在数据库中。这是当前的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<install version="1.7" type="plugin" group="system">
<name>Add Outkast</name>
<author>My Name</author>
<creationDate>November 2013</creationDate>
<copyright>Copyright (C) 2013 All rights reserved.</copyright>
<license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
<authorEmail>my@email.com</authorEmail>
<authorUrl>myURL.com</authorUrl>
<version>1.7</version>
<description>This Plugin adds Outkast's Video Hey Ya.</description>

<files>
    <filename plugin="outkastplugin">outkastplugin.php</filename>
</files>

</install>

1 个答案:

答案 0 :(得分:2)

是的,我采用了不同的方法。我是使用一个在安装时执行的SQL文件来完成的。

以下是一切:

<强>结构:

file - outkastplugin.xml
file - outkastplugin.php
folder - sql 
  >> file - install.mysql.utf8.sql

<强> outkastplugin.xml

<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="plugin" group="system" method="upgrade">
   <name>Add Outkast</name>
   <author>My Name</author>
   <creationDate>November 2013</creationDate>
   <copyright>Copyright (C) 2013 All rights reserved.</copyright>
   <license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
   <authorEmail>my@email.com</authorEmail>
   <authorUrl>myURL.com</authorUrl>
   <version>1.7</version>
   <description>This Plugin adds Outkast's Video Hey Ya.</description>

   <files>
      <filename plugin="outkastplugin">outkastplugin.php</filename>
      <folder>sql</folder>
   </files>

   <install>
      <sql>
        <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
      </sql>
   </install>

</extension>

<强> outkastplugin.php:

<?php

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin');
jimport( 'joomla.html.parameter');

class plgSystemOutkastplugin extends JPlugin
{

    function plgSystemOutkastplugin()
    {

    }        

}
?>

<强> install.mysql.utf8.sql:

CREATE TABLE IF NOT EXISTS `#__mycomponent_outkastvideos` (
        `id` int(10) NOT NULL AUTO_INCREMENT,
        `title` varchar(255) NOT NULL,
        `link_youtube` varchar(255) NOT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

INSERT INTO `#__mycomponent_outkastvideos` (`title`, `link_youtube`) VALUES ('Outkast', 'http://www.youtube.com/embed/PWgvGjAhvIw?rel=0');

我已通过电子邮件向您发送了完整的zip包。

希望这会有所帮助:)

相关问题