2

I am using foreign key mapping in my component. Now, i want to add an error message 'Cannot delete item' whenever user tries to delete the parent item.

Just like we see in the category manager 'Delete not allowed for category Foreign Category.1 item is assigned to this category.'

Kindly help, Ruchika

2

You should implement the similar check as done for categories in CMS content plugin.

...
...
...

// See if this category has any content items
$table = '#__table_name';
$count = $this->countItemsInCategory($table, $data->get('id'));

// Show error if items are found in the category
if ($count > 0)
{
    $msg = JText::sprintf('COM_CATEGORIES_DELETE_NOT_ALLOWED', $data->get('title')) .
        JText::plural('COM_CATEGORIES_N_ITEMS_ASSIGNED', $count);

    $app->enqueueMessage($msg, 'warning');

    $result = false;
}

...
...
...

/**
 * Get count of items in a category
 *
 * @param   string   $table  The table name of component table (column is catid)
 * @param   integer  $catId  The id of the category to check
 *
 * @return  integer  Count of items found
 *
 * @since   1.0
 */
private function countItemsInCategory($table, $catId)
{
    $db = JFactory::getDbo();

    // Count the items in this category
    $query = $db->getQuery(true)
        ->select('COUNT(id)')
        ->from($table)
        ->where('catid = ' . (int) $catId);

    $db->setQuery($query);

    return (int) $db->loadResult();
}
  • Thank you so much Dmitry . I wanted to know this file only from where the message is coming. But Can you tell me where shall i implement the above code?? in my model or controller ? – user5491 Jul 8 '15 at 9:48
  • I would do it in the model. – Dmitry Rekun Jul 8 '15 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy