codacopia
codacopia

Reputation: 2501

How to Insert Value to Joomla 2.5 DB with Custom Plugin

I am trying to create a joomla plugin which will insert a value into a database table for a Joomla component. I have successfully built and installed the plugin through the Joomla Extension Manager, but when I enable the plugin, I am not seeing the value inserted in the database table. Here is my php code from the plugin to review:

<?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;

}        

}
?>

Edit Update 11/07/13: I am adding the xml file from the plugin to help give some more context to the question. Ultimately I have the combo of the php file above and the xml below to create the entire plugin package. When I install the component on a Joomla 2.5 site, it installs with no errors. Then, when I enable the plugin, I get two instances of the video inserted into the database. When I disable the plugin I will get one instance of the video inserted into the database. This is not what I am after.

Ideally, when the user installs the plugin, I would like it to automatically enable the plugin. If the plugin is enabled, then the video will show up in the database. If the plugin is disabled, then the video will be not show up in the database. Here is the current xml code:

<?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>[email protected]</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>

Upvotes: 0

Views: 864

Answers (1)

Lodder
Lodder

Reputation: 19743

Right, I went for a different approach with this. I did it using an SQL file which gets executed upon installation.

Here is what everything looks like:

Structure:

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>[email protected]</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');

I have sent you the full zip package via e-mail.

Hope this helps :)

Upvotes: 2

Related Questions