Actions

Difference between revisions of "Creating content using JTableContent"

From Joomla! Documentation

(Properties of the object)
Line 29: Line 29:
 
==Data Validation==
 
==Data Validation==
 
JTableContent provides a data validation method, [[API17:JTableContent::check|JTableContent::check]]. Using this method, we can validate our data, or raise a notice if it fails.
 
JTableContent provides a data validation method, [[API17:JTableContent::check|JTableContent::check]]. Using this method, we can validate our data, or raise a notice if it fails.
 +
 +
<source lang="php">
 +
if (!$article->check()) {
 +
JError::raiseNotice(500, $article->getError());
 +
 +
return FALSE;
 +
}
 +
</source>
 +
 +
== Saving the article==
 +
Finally, we use [[API17:JTableContent::store|JTableContent::store]] to save the article.
  
 
<source lang="php">
 
<source lang="php">
Line 38: Line 49:
 
</source>
 
</source>
  
 +
== Version Note==
 +
JTableContent is not autoloaded prior to Joomla! version 3.0, so it needs to included.
  
 +
<source lang="php">
 +
JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table');
 +
</source>
  
 +
==Complete Example==
 +
<source lang="php">
 +
if (version_compare(JVERSION, '3.0', 'lt')) {
 +
JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table');
 +
}
 +
 +
$article->title            = 'This is my super cool title!';
 +
$article->alias            = JFilterOutput::stringURLSafe('This is my super cool title!');
 +
$article->introtext        = '<p>This is my super cool article!</p>';
 +
$article->catid            = 9;
 +
$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        = '*';
 +
 +
// Check to make sure our data is valid, raise notice if it's not.
 +
if (!$article->check()) {
 +
JError::raiseNotice(500, $article->getError());
 +
 +
return FALSE;
 +
}
 +
 +
// Now store the article, raise notice if it doesn't get stored.
 +
if (!$article->store(TRUE)) {
 +
JError::raiseNotice(500, $article->getError());
 +
 +
return FALSE;
 +
}
 +
</source>
 
[[Category:JTable]]
 
[[Category:JTable]]

Revision as of 20:35, 16 October 2013

Copyedit.png
This Article Needs Your Help

This article is tagged because it NEEDS REVIEW. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.


Contents

Introduction

While it is possible to add content to the database directly via queries, using JTableContent is the preferred method as it will not only simplify the task for you, but provides additional methods for checking and saving that content, not to mention other benefits.

The Object

Using JTableContent begins by creating a new instance

$article = JTable::getInstance('content');

We can now define the properties of this object that our new article will consist of.

Properties of the object

Each property of the newly created object corresponds with a field in the database. For example, our article may be defined as

$article->title            = 'This is my super cool title!';
$article->alias            = JFilterOutput::stringURLSafe('This is my super cool title!');
$article->introtext        = '<p>This is my super cool article!</p>';
$article->catid            = 9;
$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         = '*';

Data Validation

JTableContent provides a data validation method, JTableContent::check. Using this method, we can validate our data, or raise a notice if it fails.

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

Saving the article

Finally, we use JTableContent::store to save the article.

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

Version Note

JTableContent is not autoloaded prior to Joomla! version 3.0, so it needs to included.

JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table');

Complete Example

if (version_compare(JVERSION, '3.0', 'lt')) {
        JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table');
}
 
$article->title            = 'This is my super cool title!';
$article->alias            = JFilterOutput::stringURLSafe('This is my super cool title!');
$article->introtext        = '<p>This is my super cool article!</p>';
$article->catid            = 9;
$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         = '*';
 
// Check to make sure our data is valid, raise notice if it's not.
if (!$article->check()) {
        JError::raiseNotice(500, $article->getError());
 
        return FALSE;
}
 
// Now store the article, raise notice if it doesn't get stored.
if (!$article->store(TRUE)) {
        JError::raiseNotice(500, $article->getError());
 
        return FALSE;
}