0

I followed the MVC Tutorial from Joomla as close as possible to create my own component. I included Joomlas Tags system within my component using the Observer technique in the JTable class

class ComponentTableItem extends JTable
{
  function __construct(&$db)
  {
    parent::__construct('#__cmp_item', 'id', $db);
    JTableObserverTags::createObserver($this, array('typeAlias' => 'com_component.item'));
 }
}

Inserting new items and adding Tags works fine. But when I delete the item, the tags associations are not deleted. I use the following code to delete multiple entries in Model/Items.php:

public function delete(&$pks)
{
    $db = JFactory::getDbo();

    $pks = (array)$pks;

    // Iterate the items to delete each one.
    foreach ($pks as $i => $pk) {
        $item = JTable::getInstance('item', 'ComponentTable');
        $item->load($pk);
        //...do some stuff
        //and finally delete the item
        $item->delete($pk);
     }
 }

I was under the impression that the ObserverClass in the JTable class would take care of it, but that doesn't seem to be the case. Any pointers?

  • May I ask why it is important to delete the tags? – mickmackusa May 22 at 12:43
  • Sorry, I was probably unprecise. The tag association is not removed which results that the tag component shows items that have been deleted (which then results in an error 404) – Yosh May 22 at 12:57
1

I figured it out.

Instead of using the JTable class, I import the model gameModel as described here. The by deleting the item via the model, the tag-associations are removed as well. This is the code change:

    public function delete(&$pks)
    {
    jimport('joomla.application.component.model');
    JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_mycomponent/models');
    $db = JFactory::getDbo();

    $pks = (array)$pks;

    // Iterate the items to delete each one.
    foreach ($pks as $i => $pk) {
        $item = JModelLegacy::getInstance( 'Item', 'ComponentModel' );
        $item->getItem($pk);
        //...do some stuff
        //and finally delete the item
        $item->delete($pk);
     }
 }
  • It is important that the crux of your solution exists in a static fashion, here, on this site. Answers that express the resolution as a hyperlink are fragile / not encouraged because if that page moves or that site dies, the value in the answer is largely depleted. Reference links are good, but it would be better to also post your actual implementation of the referenced guidance as copy-pasted text in your answer (via an edit) -- this puts the solution in context. Beyond that, please remember to mark your answer with the green tick when the system permits you to do so. – mickmackusa May 23 at 20:08

Your Answer

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

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