Håkan Bylund
Håkan Bylund

Reputation: 64

Insert a k2 article into joomla database with php

I want to add an K2 article to a joomla DB with php. Can I just add a record to the article-table? Or do I I need to update any other table as well? I have some fundamental knowledge of PHP and mySQL, but I would appreciate a nudge in the right direction for the correct syntax to do this.

Many thanks, Håkan

Upvotes: 2

Views: 2875

Answers (3)

user1905478
user1905478

Reputation: 21

I've done this several times in Joomla 2.5 and later. Simply insert properly structured rows into the k2_items table in your Joomla database. You'll want to make sure each new row that gets added has a properly incremented id, though. Here's an example:

$data =new stdClass();
$data->id = null;
$data->title = $title;
$temp = strtolower($data->title);
$data->alias = str_replace(' ', '-', $temp);
$data->catid = $catid;
$data->published = 1;
$data->introtext = $introtext;
$data->fulltext = $fulltext;

Do the same with all the other K2 fields (there's a bunch of them) until you finally can write the following code:

$db = JFactory::getDBO();
$db->insertObject( '#__k2_items', $data, id );

Upvotes: 0

Elin
Elin

Reputation: 6770

For an article in Joomla 1.6 or later you must have an entry in the asset table as well and it has to be correctly created using the methods from JTableContent.

Upvotes: 0

Viktor Tango
Viktor Tango

Reputation: 453

I am doing the same but on K2 item save (not for the already saved items in K2). So that I can duplicate the items in K2 into articles in joomla, in one click on save on the K2 item creation panel.

An "item" in K2 is an "article" in Joomla.

I am overriding core K2 using the process I have mentioned in this post-> Joomla - Overriding getItem method

Then I have found out where the K2 saves the article, as in this post-> Joomla - Where is the code in K2 which saves a new item's Title and alias

Now, my next step will be to add a piece code to the override file's (K2's models\item.php) save() function, so as to save the same K2 item into joomla com_content table as well. You will find all the info in the posts I have mentioned. If something is not clear, leave a comment so I can revert to the query.

Good Luck!

Upvotes: 2

Related Questions