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

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

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改