2

I am creating articles in Joomla! programmatically using JTable. Since I have a lot of articles that need to get synchronized periodically, I need to check each article if it already exists before inserting it (otherwise it produces errors).

What is the best way to do so?

My idea was to retrieve all articles from database and compare unique fields. But problems (blank page) occured while retreiving the articles. Here is the code:

function getExistingArticles(){

    // Create a new query object.
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);

    $query->select('*')->where('`a.created_by_alias`= `article_synchronizer`'); // Prepare query.

    $query->from('`#___categories` AS a');

    // Reset the query using newly populated query object.
    $db->setQuery($query);
    $articles = $db->loadObjectList(); // Execute query, return result list.

    return $articles;
}

If this is the "best" way in Joomla! to check if a certain article already exists, where is the problem in this code, that results in a blank page?

Otherwise which is the best way to check if a Joomla! article with a certain content already exists?

1 Answer 1

1

I haven't tested your query, but I would suggest quoting column names and values using Joomla's API like so:

function getExistingArticles()
{
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);

    $query->select($db->quoteName(array('a.*')))
          ->from($db->quoteName('#__categories', 'a'))
          ->where($db->quoteName('a.created_by_alias') . ' = ' . $db->quote('article_synchronizer')); // Quoted query.

    $db->setQuery($query);
    $articles = $db->loadObjectList();

    return $articles;
}

I've fixed your query a little. You were using 3 underscores when defining the table prefix (only 2 should be used). The where and from clauses were also the wrong way round.

2
  • Looks much better now! I needed to add the closing bracket (also edited your post, but in peer review process). I used your changes but still receive a blank page as result. Mar 6, 2015 at 15:30
  • Approved your edit. Blank page might be due to where you have put your function and/or how you're calling it. Can you edit your question to show where you are putting your function and where/how you're calling it?
    – Lodder
    Mar 6, 2015 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.