Advertisement

[PHP][Databases] Programmatically add tags

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
ArtemisPlayer
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Nov 26, 2020 6:36 pm

[PHP][Databases] Programmatically add tags

Post by ArtemisPlayer » Thu Nov 26, 2020 6:52 pm

Hello,
I'm a beginner in web development and so with Joomla!. I'm creating a module in order to speed up article publishing. I need to add programmatically articles and tags. I managed to upload articles with this code I copied out of somewhere:

Code: Select all

	function addArticle($title, $filename){

		
		$article = JTable::getInstance('content');
		
		$article->title            = $title;
		$article->alias            = JFilterOutput::stringURLSafe($title);
		
		$article->introtext        ='text';
		$article->catid            = 15; //pages
		$article->created          = JFactory::getDate()->toSQL();
		$article->created_by_alias = 'Super User';
		$article->state            = 1;
		$article->access           = 1;
		$article->metadata         = '{"page_title":"","author":"","robots":""}';
		$article->language         = '*';
		
		
		if (!$article->check()) {
			JError::raiseNotice(500, $article->getError());
	 
			return FALSE;
		}
		
		if (!$article->store(TRUE)) {
			JError::raiseNotice(500, $article->getError());
	 
			return FALSE;
		}
		return TRUE;
	 
	}
It works, but i don't really understand why

Code: Select all

$article = JTable::getInstance('content');
does.
And when i'm trying to do the same thing for tags:

Code: Select all

	function addTag($tagName){
		$tag = JTable::getInstance('tags');
		
		$tag->path          = JFilterOutput::stringURLSafe($tagName);
		$tag->title 		= $tagName;
		$tag->level			= 1;
		$tag->alias			= JFilterOutput::stringURLSafe($tagName);
		$tag->published		= 1;
		$tag->publish_up	= JFactory::getDate()->toSQL();
		$tag->version 		= 1;
		$tag->language		= '*';
		$tag->access		= 1;
		$tag->created_time	= JFactory::getDate()->toSQL();

		
 		if (!$tag->check()) {
			JError::raiseNotice(500, $tag->getError());
	 
			return FALSE;
		} 
		
		if (!$tag->store(TRUE)) {
			JError::raiseNotice(500, $tag->getError());
	 
			return FALSE;
		}
		return TRUE;
	}

I got the error .check() method doesn't exist. I don't understand why: is there a diffence between these two functions ? In both cases I'm adding a row in a SQL database.

Thank you for any help in understanding the problem.
PS: English isn't my native language please excuse me if some sentences sound weird.

EDIT: is there a way to display the code with colors ?
Last edited by toivo on Thu Nov 26, 2020 7:38 pm, edited 1 time in total.
Reason: mod note: moved from 3.x General Questions

Advertisement
Advertisement
Locked

Return to “Joomla! 3.x Coding”