Php 使用Joomla删除另一个表中的多行
我在网上搜索了这个问题的好答案,但没有结果。我试图删除另一个表中的多行,而不是从正在执行组件的表中删除Php 使用Joomla删除另一个表中的多行,php,mysql,joomla,Php,Mysql,Joomla,我在网上搜索了这个问题的好答案,但没有结果。我试图删除另一个表中的多行,而不是从正在执行组件的表中删除 <?php // No direct access to this file defined('_JEXEC') or die('Restricted access'); // import Joomla modelform library jimport('joomla.application.component.modeladmin'); /** * Modeling M
<?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); } }
基本上,当我在组件A中删除4行时,这些行也必须在组件B中删除。键存在于另一个表中,所以这不是问题。我可以很容易地用一些快速而肮脏的mysql查询来完成它,但我想用Joomla的内置方法来完成 在joomla中,从JControllerAdmin使用delete方法,其中使用getModel抓取一个模型,然后执行另一个delete方法。但我似乎找不到这个删除方法的位置,它实际上删除了行 顺便说一句,我已经从JControllerAdmin复制粘贴了delete方法,并将其粘贴到我自己的控制器中。我确实改了名字,但一切正常 所以现在我求助于Stackoverflow来解决我的问题。长话短说:我有一个customDelete()方法,它是JControllerAdmin类中delete方法的相同副本,我想添加一些功能,允许我使用customDelete()方法中的id来删除另一个表中的行 我希望这是清楚的:) 谢谢 编辑: 这是控制器中的删除方法。我必须删除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 { // } }
(对应于组件B的表)中包含id的所有行#uu modeling
$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()); } }
这并不难 基本上,您需要做的就是调用与#uu建模表相关的不同模型。因此,您需要一个模型,我们可以称之为建模,该模型如下所示:公共函数customDelete(){ //检查请求是否伪造 JSession::checkToken()或die(JText::(('jinfalid_TOKEN')); //获取要从请求中删除的项目。 $cid=JRequest::getVar('cid',array(),'','array'); 如果(!is_数组($cid)|计数($cid)<1){ JError::raiseWarning(500,JText::(($this->text_prefix.'u NO_ITEM_SELECTED')); }否则{ //去拿模型。 $model=$this->getModel(); //确保项目ID是整数 jimport('joomla.utilities.arrayhelper'); JArrayHelper::toInteger($cid); //移除项目。 如果($model->delete($cid)){ $this->setMessage(JText::复数($this->text_前缀。“\u N_ITEMS_DELETED”,count($cid)); }否则{ $this->setMessage($model->getError()); } }
是要从jtable中删除的表吗?当然,您可以始终使用jdatabasequery编写所需的查询,但如果它是jtable,则可以创建一个实例。
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 { // } }