2013-04-20 96 views
2

我已经在网上搜索了一个很好的答案,但没有结果。我试图删除另一个表中的多行而不是执行该组件的位置。用Joomla删除另一个表中的多行

基本上,当我删除组件A中的4行时,这些行也必须在组件B中删除。该键存在于另一个表中,所以这不是问题。我可以很容易地做到这一点与一些快速和肮脏的MySQL查询,但我想用Joomla的内置方法做到这一点。

在joomla中,从JControllerAdmin中使用delete方法,其中使用getModel获取模型,然后执行另一个删除方法。但我似乎无法找到这个删除方法所在的位置,它实际上删除了这些行。

顺便说一句我有复制粘贴从JControllerAdmin的删除方法,并将其粘贴在我自己的控制器。我确实改名,但一切正常

所以,现在我已经转向了Stackoverflow来获得我的问题的帮助。长话短说:我有一个customDelete()方法,它是从JControllerAdmin类中删除方法的一个完全相同的副本,我想添加一些功能,使我可以使用customDelete()方法中的id删除另一个表中的行。

我希望这是明确的:)

谢谢!编辑: 这是控制器中的删除方法。我必须从#__modeling删除所有的行包含的ID(相当于B组份表)内$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()); 
     } 
    } 
+0

是你想从一个JTable删除表?你当然可以总是使用jdatabasequery来编写你需要的查询,但如果它是一个jtable,你可以创建一个实例。 – Elin 2013-04-20 21:51:41

回答

3

这并不难。

基本上所有你需要做的就是调用一个与#__模型表相关的不同模型。所以你需要一个我们可以称之为建模的模型:

<?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); 
    } 

} 

上面的模型扩展了JModelAdmin(它有delete方法) delete()方法)。它应该在管理员/您的组件/模型中。

您还需要一个一个JTable类,如下所示:

<?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); 
    } 
} 

所以你可以看到,在餐桌上,你希望JTable类的点,从删除。这应该进入你的组件/表格文件夹。

然后,您应该能够改变你的customDelete方法如下:

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

一个

+0

我明白了!真棒! :d – Ortix92 2013-04-25 21:27:38

相关问题