Creating content using JTableContent: Difference between revisions
From Joomla! Documentation
Punctuation, markup changes. |
|||
| (3 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
{{version|2.5,3.x}} | {{version|2.5,3.x}} | ||
{{review}} | {{review}} | ||
==Introduction== | == Introduction == | ||
JTableContent is the preferred method to add content to the database rather than with direct queries. JTableContent has many benefits which include simplifying the methods to ''create, read, update | JTableContent is the preferred method to add content to the database rather than with direct queries. JTableContent has many benefits which include simplifying the methods to ''create'', ''read'', ''update'' and ''delete'' content. | ||
==The Object== | ==The Object== | ||
Using JTableContent begins by creating a new instance | Using JTableContent begins by creating a new instance | ||
< | <syntaxhighlight lang="php"> | ||
$article = JTable::getInstance('content'); | $article = JTable::getInstance('content'); | ||
</ | </syntaxhighlight> | ||
We can now define the properties of this object that our new article will | We can now define the properties of this object that our new article will have. | ||
==Properties of the | == 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 | Each property of the newly created object corresponds with a field in the database. For example, our article may be defined as | ||
< | <syntaxhighlight lang="php"> | ||
$article->title = 'This is my super cool title!'; | $article->title = 'This is my super cool title!'; | ||
$article->alias = JFilterOutput::stringURLSafe('This is my super cool title!'); | $article->alias = JFilterOutput::stringURLSafe('This is my super cool title!'); | ||
| Line 25: | Line 25: | ||
$article->metadata = '{"page_title":"","author":"","robots":""}'; | $article->metadata = '{"page_title":"","author":"","robots":""}'; | ||
$article->language = '*'; | $article->language = '*'; | ||
</ | </syntaxhighlight> | ||
To keep this example simple, we have used only 10 properties out of the 30. To explore other properties and their data types, look at the ''#__content'' table in your database. | |||
< | == 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. | |||
<syntaxhighlight lang="php"> | |||
if (!$article->check()) { | if (!$article->check()) { | ||
JError::raiseNotice(500, $article->getError()); | JError::raiseNotice(500, $article->getError()); | ||
| Line 36: | Line 38: | ||
return FALSE; | return FALSE; | ||
} | } | ||
</ | </syntaxhighlight> | ||
== Saving the | == Saving the Article== | ||
Finally, we use [[API17:JTableContent::store|JTableContent::store]] to save the article. | Finally, we use [[API17:JTableContent::store|JTableContent::store]] to save the article. | ||
< | <syntaxhighlight lang="php"> | ||
if (!$article->store(TRUE)) { | if (!$article->store(TRUE)) { | ||
JError::raiseNotice(500, $article->getError()); | JError::raiseNotice(500, $article->getError()); | ||
| Line 47: | Line 49: | ||
return FALSE; | return FALSE; | ||
} | } | ||
</ | </syntaxhighlight> | ||
== Version Note== | == Version Note== | ||
JTableContent is not autoloaded prior to Joomla! version 3.0, so it needs to included. | JTableContent is not autoloaded prior to Joomla! version 3.0, so it needs to be included. | ||
< | <syntaxhighlight lang="php"> | ||
JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table'); | JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table'); | ||
</ | </syntaxhighlight> | ||
==Complete Example== | == Complete Example == | ||
< | <syntaxhighlight lang="php"> | ||
if (version_compare(JVERSION, '3.0', 'lt')) { | if (version_compare(JVERSION, '3.0', 'lt')) { | ||
JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table'); | JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table'); | ||
} | } | ||
$article = JTable::getInstance('content'); | |||
$article->title = 'This is my super cool title!'; | $article->title = 'This is my super cool title!'; | ||
| Line 73: | Line 77: | ||
$article->language = '*'; | $article->language = '*'; | ||
// Check to make sure our data is valid | // Check to make sure our data is valid; raise notice if it is not. | ||
if (!$article->check()) { | if (!$article->check()) { | ||
JError::raiseNotice(500, $article->getError()); | JError::raiseNotice(500, $article->getError()); | ||
| Line 80: | Line 84: | ||
} | } | ||
// Now store the article | // Now store the article; raise notice if it doesn't get stored. | ||
if (!$article->store(TRUE)) { | if (!$article->store(TRUE)) { | ||
JError::raiseNotice(500, $article->getError()); | JError::raiseNotice(500, $article->getError()); | ||
| Line 86: | Line 90: | ||
return FALSE; | return FALSE; | ||
} | } | ||
</ | </syntaxhighlight> | ||
[[Category:JTable]] | [[Category:JTable]] | ||
Latest revision as of 14:52, 1 September 2022
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.
Introduction
JTableContent is the preferred method to add content to the database rather than with direct queries. JTableContent has many benefits which include simplifying the methods to create, read, update and delete content.
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 have.
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 = '*';
To keep this example simple, we have used only 10 properties out of the 30. To explore other properties and their data types, look at the #__content table in your database.
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 be 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 = JTable::getInstance('content');
$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 is 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;
}