Hello. The site adminpanel implements the functionality of mass category assignment to the list of items, which replaces the current category value for the selected list with a new one. How to make the function add new values ​​to the old, and not replace them? For example, there are 5 elements that are in category A, how to make it so that when adding categories B and C, we get all three ABCs. Below is the code that performs the process of mass association of categories to the selected elements. (I apologize for the amateurish formulation). thank

public function massxref_cats(){ $this->massxref('massxref'); } public function massxref_cats_exe(){ $virtuemart_cat_ids = vRequest::getInt('cid', array() ); $session = JFactory::getSession(); $cids = json_decode($session->get('vm_product_ids', array(), 'vm'),true); $productModel = VmModel::getModel('product'); foreach($cids as $cid){ $data = array('virtuemart_product_id' => $cid, 'virtuemart_category_id' => $virtuemart_cat_ids); $data = $productModel->updateXrefAndChildTables ($data, 'product_categories',TRUE); } $this->massxref('massxref_cats'); } public function massxref($layoutName){ vRequest::vmCheckToken(); $cids = vRequest::getInt('virtuemart_product_id'); if(empty($cids)){ $session = JFactory::getSession(); $cids = json_decode($session->get('vm_product_ids', '', 'vm'),true); } else { $session = JFactory::getSession(); $session->set('vm_product_ids', json_encode($cids),'vm'); } if(!empty($cids)){ $q = 'SELECT `product_name` FROM `#__virtuemart_products_' . VmConfig::$vmlang . '` '; $q .= ' WHERE `virtuemart_product_id` IN (' . implode(',', $cids) . ')'; $db = JFactory::getDbo(); $db->setQuery($q); $productNames = $db->loadColumn(); vmInfo('COM_VIRTUEMART_PRODUCT_XREF_NAMES',implode(', ',$productNames)); } $this->addViewPath(VMPATH_ADMIN . DS . 'views'); $document = JFactory::getDocument(); $viewType = $document->getType(); $view = $this->getView($this->_cname, $viewType); $view->setLayout($layoutName); $view->display(); } 

    1 answer 1

    In the case when there are many elements that have 1 or more categories, and when one category can contain many related elements, it is best to use the many-to-many connection. For this, a connected table is created, for example, elements_categories with two primary keys in the form of element_id and category_id, where the id of the element and the category id are entered. And then requests get the necessary element with the categories connected with it. In this case, when adding new elements with their categories, the old ones will not be deleted, for each value in the elements_categories table will be unique.

    • The fact is that the multcategory functionality is implemented. When editing an individual item, you can add as many categories as you like to it. With a mass association, you can also add as many categories as you wish for the item list, but in this case the previous values ​​are replaced. I think that everything lies in this code, which I submitted, but I can be wrong because I am not strong in this (something is done like the path, but it is necessary to update :)) - Anna
    • the functionality of this action provides for the assignment of multiple categories, but it is very important that the values ​​are added and not replaced. thank. - Anna
    • @Anna This is problematic because you need to make selects to select old values ​​and append, when editing a separate element at the front, the category to which it belongs is selected, i.e. The functionality updates the line but completely. - Naumov
    • @Naumov pity :( but, since you said (or perhaps in some other way or another) such functionality is present when editing each element separately, you can also tie it to the mass association - Anna
    • @Naumov may need to try to find the code for editing the categories of an individual element and in its studio? - Anna