我尝试从PHP脚本中插入一些文章,但我不明白。我需要知道哪些字段是必需的,以及如何创建查询才能做到这一点。
现在,我写下一个代码:
foreach($RSS_DOC->channel->item as $RSSitem){
$alias = md5($RSSitem->title);
$fetch_date = date("Y-m-j G:i:s"); //NOTE: we don't use a DB SQL function so its database independent
$item_title = $RSSitem->title;
$item_description = $RSSitem->description;
$item_date = date("Y-m-j G:i:s", strtotime($RSSitem->pubDate));
$item_url = $RSSitem->link;
// Does record already exist? Only insert if new item...
$item_exists_sql = "SELECT alias FROM jos_content where alias = ".$alias;
$item_exists = mysqli_query($enlace,$item_exists_sql);
if(mysqli_num_rows($item_exists)<1){
$mensaje = "<p>".$item_description."</p>Publicado: ".$item_date."<br/><a href='".$item_url."'>Ver noticia original</a>";
$item_insert_sql = "INSERT INTO jos_content(title, alias, introtext, state, catid, created, created_by, access,featured) VALUES ('" .$item_title. "', '" . $alias. "', '" . $mensaje. "',1, 10,'".$fetch_date."',448,1,1)";
$insert_item = mysqli_query($enlace,$item_insert_sql);
}
}
首要任务是创建商品数据对象。我使用一种方法来清除现有的id或asset_id引用,以防我将文章数据从一个实例迁移到另一个实例。也可以用您的逻辑来代替,以构建商品数据对象。只要确保使用关联数组即可:
function processArticleData($obj) {
$data = array();
foreach ($obj as $key => $value) {
$data[$key] = $value;
}
$data['id'] = 0;
unset($data['asset_id']);
return $data;
}
接下来,加载JTable内容类,绑定数据并保存。 Joomla完成了所有其余工作:
function addArticle($obj) {
// Initialise variables;
$data = processModuleData($obj);
$table = &JTable::getInstance('content');
// Bind the data.
if (!$table->bind($data))
{
echo "<h1>Error Binding Article Data</h1>";
return false;
}
// Check the data.
if (!$table->check())
{
echo "<h1>Error Checking Article Data</h1>";
return false;
}
// Store the data.
if (!$table->store())
{
echo "<h1>Error Storing Article Data</h1>";
return false;
}
return $table->get('id');
}
这种方法的好处是,它消除了对必填字段或潜在错误的任何“猜测”,就好像数据存在问题时,Joomla会抛出异常或错误说明问题所在。如果希望/需要获得真正的幻想,甚至可以加载JForm对象的内容,将数据绑定到该对象,然后在绑定到JTable对象之前进行验证。
创建数据对象只有两个要求。第一种是使用关联数组,第二种是所有键名都与#__content表中的列匹配。示例数据对象如下所示:
$data = array(
'title' => $title,
'alias' => $alias,
'introtext' => $introtext,
'fulltext' => $fulltext,
'catid' => $catid,
'images' => '',
'urls' => '',
'attribs' => '',
'metakey' => '',
'metadesc' => '',
'metadata' => '',
'language' => '',
'xreference' => '',
'created' => JFactory::getDate()->toSql(),
'created_by' => JFactory::getUser()->id,
'publish_up' => JFactory::getDate()->toSql(),
'publish_down' => JFactory::getDbo()->getNullDate(),
'state' => 1
);
[我使用更多Joomla辅助函数使我的工作更轻松,但这应该为您提供一个很好的起点。
*编辑*
我改正了错字。不知道是否复制/粘贴但将下面的数组声明切换到下面的行并再次测试:
$data = processModuleData($obj);
此脚本应执行此操作。...假定它存在于您网站的根目录中:
if (!defined('_JEXEC')) {
define( '_JEXEC', 1 );
define ('JPATH_BASE', 'c:\\wamp\\www\\mysiteroot');
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
$mainframe = JFactory::getApplication('site');
}
function getContentTable($type = 'Content', $prefix = 'JTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
function addArticle($title, $alias)
{
$table = getContentTable();
$table->title = $title;
$table->alias = $alias;
$table->catid = 2;
$table->state = 1;
// and so on!
// then save it
$table->store();
}
$result = addArticle("foo", "bar");