【问题标题】:Magento model can't delete rowMagento 模型无法删除行
【发布时间】:2015-01-08 07:44:36
【问题描述】:

我正在尝试使用来自观察者的以下代码删除我的数据库表行。但无法删除它会在系统日志中出现错误

 Some transactions have not been committed or rolled back

$customFieldValue =  $this->_getRequest()->getPost();
             //$groupCodeArray = $customFieldValue['product']['customer_group_ids'];

             $DataCollectionDel = Mage::getModel('customerspecificproduct/customerspecificproduct')->getCollection()->addFieldToFilter(
                'product_ids',
                array(
                    'eq'=>$product->getId()
                )
            )->addFieldToFilter(
                'group_id',
                array(
                    'notnull'=>true,
                )
            );


            if($DataCollectionDel){
             foreach($DataCollectionDel as $data){
                    $deleteRow = Mage::getModel('customerspecificproduct/customerspecificproduct')->load($data->getId())->delete();

                }
             }exit;

【问题讨论】:

  • 使用 try catch 修改你的代码。在 catch 块中打印或记录异常。因为可能有例外的可能
  • 可能是您要删除的行是另一个表中的外键。

标签: magento model magento-1.9.1


【解决方案1】:

我自己找到了解决办法

$DataCollectionDel = Mage::getModel('customerspecificproduct/customerspecificproduct')->getCollection()->addFieldToFilter(
                'product_ids',
                array(
                    'eq'=>$product->getId()
                )
            )->addFieldToFilter(
                'group_id',
                array(
                    'notnull'=>true,
                )
            );



             try{
                if($DataCollectionDel){
             foreach($DataCollectionDel as $data){
                    $data->delete();
                }
             }

【讨论】:

    【解决方案2】:

    我找到了解决方案:3 没有 getCollection()。 型号

     public function deleteByCondition($itemId,$vendor)
        {
            return $this->getResource()->deleteByCondition($itemId,$vendor);
        }
    

    资源模型

      public function deleteByCondition($itemId,$vendor)
        {
            $table = $this->getMainTable();
            $where = array();
            $where[] =  $this->_getWriteAdapter()->quoteInto('item_id = ?',$itemId);
            $where[] =  $this->_getWriteAdapter()->quoteInto("vendor = ? ", $vendor);
            $result = $this->_getWriteAdapter()->delete($table,$where);
            return $result;
        }
    

    【讨论】:

      猜你喜欢
      • 2022-11-02
      • 2021-03-16
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 2014-03-13
      相关资源
      最近更新 更多