1

I´m still new to Joomla and to coding SQL.

I was thinking about how I should manage a big system I made with information that is saved in a sql table, that contains some user input and the "id" of an article.

If you or another administrator deletes an article, I guess I have to delete the row containing that articles id in my custom sql table too?

Is there a way to make a script that gets executed every time an article gets deleted? that way I could maybe get the article id and then remove it from my custom table.

How do you guys handle your custom sql tables that contains references to articles or users in Joomla?

1
  • 1
    I suppose you are seeking a TRIGGER. Continue your researching using this keyword. While they are handy because you don't need to modify your php codes (you can easily create them in phpMyAdmin), they can - in some cases - be harder to debug and cause unexpected results when your php code is seeking an affected row count. Your question is non-specific, so providing a concrete answer is difficult. If possible, please offer more details so that your question can be answered. Sep 4, 2018 at 21:28

1 Answer 1

2

Create a plugin with onContentAfterDelete event. Basic example:

<?php
defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;

class PlgContentExample extends CMSPlugin
{
    public function onContentAfterDelete($context, $item)
    {
        // Check that the deleted item is an article.
        if ($context !== 'com_content.article')
        {
            return true;
        }

        $db = Factory::getDbo();
        $query = $db->getQuery(true)
            ->delete($db->quoteName('#__customtable'))
            ->where($db->quoteName('article_id') . ' = ' . (int) $item->id);
        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (\RuntimeException $e)
        {
            // Informational log only
        }
    }
}

Replace #__customtable with the name of your table and article_id with the column that holds article IDs.

Docs:

Plugin/Events/Content

Creating a content plugin

Plugin Development

0

Your Answer

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

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