Keyword

how to insert additional data in the database when creating an item

  • doberk
  • doberk's Avatar Topic Author
  • Offline
  • New Member
More
6 years 3 months ago #168633 by doberk
Hello, I would like to load a php script right after save a new item. What I want to do is to insert additional data in the database at the same time the backend save the item information in database. I mean, save button should save the item and also run my script in order to save other data in other table in joomla database.

I was taking a look the the k2 files, but I don't know what file I have to override? Could you help me?

Thanks in advance.

Please Log in or Create an account to join the conversation.

More
6 years 3 months ago #168638 by Sambroza
You can do this with a K2 plugin.
Find here an example plugin: github.com/getk2/k2-example-plugin/blob/master/example.php

You can hook your additional actions on the
onAfterK2Save
method.
function onAfterK2Save($row, $isNew)
	{
		// Do something here
	}

Please Log in or Create an account to join the conversation.

  • doberk
  • doberk's Avatar Topic Author
  • Offline
  • New Member
More
6 years 3 months ago #168639 by doberk
Thank you very much, very useful info, I'll try it :)

Please Log in or Create an account to join the conversation.

  • doberk
  • doberk's Avatar Topic Author
  • Offline
  • New Member
More
6 years 2 months ago - 6 years 2 months ago #169173 by doberk

Sambroza wrote: You can do this with a K2 plugin.
Find here an example plugin: github.com/getk2/k2-example-plugin/blob/master/example.php

You can hook your additional actions on the

onAfterK2Save
method.
function onAfterK2Save($row, $isNew)
	{
		// Do something here
	}


Hello, I was trying to create a plugin, but it doesn't work for me. This is my plugin code:

file blogNotifyPlugin.php:
<?php

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

// Load the K2 Plugin API
JLoader::register('K2Plugin', JPATH_ADMINISTRATOR.'/components/com_k2/lib/k2plugin.php');

// Initiate class to hold plugin events
class blogNotifyPlugin extends K2Plugin {
    
    public $pluginName = 'blogNotifyPlugin';
    public $pluginNameHumanReadable = 'Blog Notify Plugin';

    public function __construct(&$subject, $params){
        parent::__construct($subject, $params);
    }

    public function onAfterK2Save(&$row, $isNew){
        echo '<script>alert("onAfterK2Save event");</script>';
		die();
		return true;
    }
	
}

and file blogNotifyPlugin.xml:
<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="plugin" group="k2" method="upgrade">
	<name>Blog Notify Plugin</name>
	<author>Doberk</author>
	<creationDate>18/09/2018</creationDate>
	<copyright></copyright>
	<authorEmail></authorEmail>
	<authorUrl></authorUrl>
	<version>1.0</version>
	<description>test plugin</description>
	<files>
		<filename plugin="blogNotifyPlugin">blogNotifyPlugin.php</filename>
	</files>
</extension>

The plugin was installed from the backend using joomla installer, and installation worked good. The files was uploaded to /plugins/k2/blogNotifyPlugin folder. I have published the plugin with public access after installation. It should show a popup after creating new items, but nothing happens. Where am I wrong? I create new items from backend.

Thanks.
Last edit: 6 years 2 months ago by doberk.

Please Log in or Create an account to join the conversation.

  • Mohamed Abdelaziz
  • Mohamed Abdelaziz's Avatar
  • Offline
  • Platinum Member
  • Joomla Developer
More
6 years 2 months ago #169177 by Mohamed Abdelaziz
Change the class name to 'plgblogNotifyPlugin'

Multiple Extra Fields Groups for K2
AutoMeta for K2
Chained Fields for K2
More K2 Extensions In My Extensions Store

Please Log in or Create an account to join the conversation.

More
6 years 2 months ago #169181 by JoomlaWorks
You don't see anything because a redirect (POST to GET) is performed when you save the item.

If you want to show a message after saving, then use the Joomla message API (docs.joomla.org/Display_error_messages_and_notices) for that.

That event is great when building a K2 plugin with its own DB table(s) and you want e.g. to grab the input from the K2 item form and process/save data in the database.

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • doberk
  • doberk's Avatar Topic Author
  • Offline
  • New Member
More
6 years 2 months ago - 6 years 2 months ago #169182 by doberk
Thank you both. The popup was only a way to test the plugin. The real target of the plugin is to send a notification mail after a new item is published.

I have change the code according your recomendations, now the method should insert a row in a table, but still does't work, no new rows are inserted after creating items. I have checked k2 settings and I have ensured k2 plugins are enabled. I keep looking for the problem, this is the updated code of my plugin:
<?php

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

// Load the K2 Plugin API
JLoader::register('K2Plugin', JPATH_ADMINISTRATOR.'/components/com_k2/lib/k2plugin.php');

// Initiate class to hold plugin events
class plgblogNotifyPlugin extends K2Plugin {
    
    public $pluginName = 'blogNotifyPlugin';
    public $pluginNameHumanReadable = 'Blog Notify Plugin';

    public function __construct(&$subject, $params){
        parent::__construct($subject, $params);
    }

    public function onAfterK2Save(&$row, $isNew){
        $query = "insert into jos_f1ld_suscripciones_blog values (null,'18/09/2018','[email protected]','0','12345')";
        $db->setQuery($query);
        $result = $db->execute();
    }
	
}

That query works good in phpmyadmin, I have checked it.
Last edit: 6 years 2 months ago by doberk.

Please Log in or Create an account to join the conversation.

  • doberk
  • doberk's Avatar Topic Author
  • Offline
  • New Member
More
6 years 2 months ago #169183 by doberk
sorry, there was a mistake in the method code, no $db defined. The right code is this, now it works good.
    public function onAfterK2Save(&$row, $isNew){
        $query = "insert into `jos_f1ld_suscripciones_blog` values (null,'18/09/2018','[email protected]','0','12345')";
        $db = JFactory::getDbo();
        $db->setQuery($query);
        $result = $db->execute();
    }

Many thanks for help :)

Please Log in or Create an account to join the conversation.

More
6 years 2 months ago #169186 by JoomlaWorks
Great then :)

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • doberk
  • doberk's Avatar Topic Author
  • Offline
  • New Member
More
6 years 2 months ago - 6 years 2 months ago #169195 by doberk

Mohamed Abdelaziz wrote: Change the class name to 'plgblogNotifyPlugin'


This is an important matter, but is inexact. Following the example plugin , the class name must be PlgK2 + plugin_name. In my case, my plugin name is blogNotifyPlugin and it didn't work until the class name was PlgK2blogNotifyPlugin. I comment it in case anybody with the same problem find this thread in the future.

R.
Last edit: 6 years 2 months ago by doberk.

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum