I've searched the internet for a good answer to this question but with no results. I'm trying to delete multiple rows in another table than from where the component is being executed.
Basically, when I delete 4 rows in component A, these rows also have to be deleted in component B. The key exists in the other table so that's not a problem. I can easily do it with some quick and dirty mysql queries but I want to do it with Joomla's built in methods.
In joomla the delete method is used from JControllerAdmin where a model is grabbed with getModel and then another delete method is executed. But I can't seem to find where this delete method is located which actually deletes the rows.
BTW I have copy pasted the delete method from JControllerAdmin and pasted it in my own controller. I did change the name but everything works
So now I have turned to Stackoverflow to get help with my problem. Long story short: I have a customDelete() method which is an identical copy of the delete method from JControllerAdmin class and I want to add functionality which allows me to use the id's which are in the customDelete() method to delete rows in another table.
I hope this is clear :)
Thanks!
EDIT: This is the delete method in the controller. I have to delete all the rows from #__modeling
(the table corresponding to Component B) containing the id's inside $cid
public function customDelete() {
// Check for request forgeries
JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));
// Get items to remove from the request.
$cid = JRequest::getVar('cid', array(), '', 'array');
if (!is_array($cid) || count($cid) < 1) {
JError::raiseWarning(500, JText::_($this->text_prefix . '_NO_ITEM_SELECTED'));
} else {
// Get the model.
$model = $this->getModel();
// Make sure the item ids are integers
jimport('joomla.utilities.arrayhelper');
JArrayHelper::toInteger($cid);
// Remove the items.
if ($model->delete($cid)) {
$this->setMessage(JText::plural($this->text_prefix . '_N_ITEMS_DELETED', count($cid)));
} else {
$this->setMessage($model->getError());
}
}
This isn't so hard.
Essentially all you need to do is to invoke a different model which relates to the #__modeling table. So you would need a model which we can call modeling which would look like:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla modelform library
jimport('joomla.application.component.modeladmin');
/**
* Modeling Model
*/
class MyModelModeling extends JModelAdmin {
/**
* Returns a reference to the a Table object, always creating it.
*
* @param type The table type to instantiate
* @param string A prefix for the table class name. Optional.
* @param array Configuration array for model. Optional.
* @return JTable A database object
* @since 1.6
*/
public function getTable($type = 'Modeling', $prefix = 'MyTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
}
The above model extends JModelAdmin (which has the delete method) and tells the delete method which table to delete from (because getTable is called by delete() method). It should go in administrator/yourcomponent/models.
You will also need an a JTable class as follows:
<?php
/**
* @package Joomla.Administrator
* @subpackage com_users
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* Modeling table class
*
* @package Joomla.Administrator
* @subpackage com_users
* @since 2.5
*/
class MyTableModeling extends JTable
{
/**
* Constructor
*
* @param JDatabaseDriver &$db Database object
*
* @since 2.5
*/
public function __construct(&$db)
{
parent::__construct('#__modeling', 'id', $db);
}
}
So you can see that the JTable class points at the table you wish to delete from. This should go into yourcomponent/tables folder.
You should then be able to change your customDelete method as follows:
public function customDelete() {
// Check for request forgeries
JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));
// Get items to remove from the request.
$cid = JRequest::getVar('cid', array(), '', 'array');
if (!is_array($cid) || count($cid) < 1) {
JError::raiseWarning(500, JText::_($this->text_prefix . '_NO_ITEM_SELECTED'));
} else {
// Get the model.
$model = $this->getModel();
// Make sure the item ids are integers
jimport('joomla.utilities.arrayhelper');
JArrayHelper::toInteger($cid);
// Remove the items.
if ($model->delete($cid)) {
$this->setMessage(JText::plural($this->text_prefix . '_N_ITEMS_DELETED', count($cid)));
} else {
$this->setMessage($model->getError());
}
// Get the modeling model
$new_model = JModelLegacy::getInstance('Modeling','MyModel');
if ($new_model->delete($cid)) {
// Items deleted from #__modeling table
} else {
//
}
}
HTH
A