2013-04-20 21 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

相關問題