Keyword

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

  • doberk
  • doberk's Avatar Topic Author
  • Offline
  • New Member
More
2 months 2 weeks ago #168633 by doberk
doberk created the topic: how to insert additional data in the database when creating an item
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
2 months 2 weeks ago #168638 by Sambroza
Sambroza replied the topic: how to insert additional data in the database when creating an item
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
2 months 2 weeks ago #168639 by doberk
doberk replied the topic: how to insert additional data in the database when creating an item
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
1 month 3 days ago - 1 month 3 days ago #169173 by doberk
doberk replied the topic: how to insert additional data in the database when creating an item

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: 1 month 3 days ago by doberk.

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

  • Mohamed Abdelaziz
  • Mohamed Abdelaziz's Avatar
  • Offline
  • Platinum Member
  • Joomla Extensions Developer
More
1 month 3 days ago #169177 by Mohamed Abdelaziz
Mohamed Abdelaziz replied the topic: how to insert additional data in the database when creating an item
Change the class name to 'plgblogNotifyPlugin'

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

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
1 month 3 days ago #169181 by Fotis
Fotis replied the topic: how to insert additional data in the database when creating an item
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.


If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

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

  • doberk
  • doberk's Avatar Topic Author
  • Offline
  • New Member
More
1 month 3 days ago - 1 month 3 days ago #169182 by doberk
doberk replied the topic: how to insert additional data in the database when creating an item
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: 1 month 3 days ago by doberk.

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

  • doberk
  • doberk's Avatar Topic Author
  • Offline
  • New Member
More
1 month 3 days ago #169183 by doberk
doberk replied the topic: how to insert additional data in the database when creating an item
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.

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
1 month 3 days ago #169186 by Fotis
Fotis replied the topic: how to insert additional data in the database when creating an item
Great then :)


If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

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

  • doberk
  • doberk's Avatar Topic Author
  • Offline
  • New Member
More
1 month 3 days ago - 1 month 3 days ago #169195 by doberk
doberk replied the topic: how to insert additional data in the database when creating an item

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: 1 month 3 days ago by doberk.

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

Moderators: william white