1

I'm trying to make my own field type and it works fine so far, but the values are not saved to the DB, where do I do that? I know there is a similar question but I'm not content with the answer.

#__items
#__tags
#__item_tags

I query just the tags to get the options and then the intersection table to get the selected ones, the item id is known already. My class inherits from JFormFieldList to get the getInput functionality. I use this field inside a form xml to define it for the view.

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

JFormHelper::loadFieldClass('list');

class JFormFieldMyTag extends JFormFieldList
{
    protected $type = 'MyTags';

    protected function getOptions()
    {
        $db = JFactory::getDbo();

        $query = $db->getQuery(true);
        $query->select('all tags')->from('tags');
        $db->setQuery($query);
        $rows = $db->loadObjectList();
        $this->value = $this->getSelected(); // The input function takes this as an array of the selected values
        return $rows;
    }

    protected function getSelected()
    {
        $db = JFactory::getDbo();

        $query = $db->getQuery(true);
        $query  ->select('only the selected ones')->from('intersection')->where('id' = $this->getAttribute('id');
        $db->setQuery($query);
        $rows = $db->loadObjectList();
        return $rows;
    }
}

What I need is a way to remove and add entries to the intersection table, joomla seems not to do it automatically and I cant find anything in the docs.

1 Answer 1

1

Of course CMS does not know about your intersection table (in your case it is #__item_tags). When you save an item CMS only updates the corresponding #__items table.

You should additionally call the method to save you tags. The best place for it is the save() method of your model. I do not see all the code of your component, but this should be something like this:

public function save($data)
{
    // Try to save the item
    if (!parent::save($data))
    {
        return false;
    }

    // The item is saved, lets save the tags
    if (!$this->saveTags($data))
    {
        return false;
    }

    return true;
}

Or you can use postSaveHook() method of the controller to run your saveTags() method. See the example in CategoriesControllerCategory.

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.