duandie0921 2013-04-20 20:58
浏览 46
已采纳

使用Joomla删除另一个表中的多个行

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());
        }
    }
  • 写回答

1条回答 默认 最新

  • down101102 2013-04-25 09:52
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算