2

I am trying to delete a data from the table.

I am using this code snippet

$categoryModel= Mage::getModel('blog/category')
        ->getCollection()
        foreach ($categoryModel as $cat) {  
                $cat->delete();
        }

and it is deleting the complete table.

but I dont want to delete the complete table so I filter the data with some attribute and tried this code

$categoryModel= Mage::getModel('blog/category')
        ->getCollection()
        ->addFieldToFilter('article_id', $articleId);  
        foreach ($categoryModel as $cat) {
                $cat->delete();
        }

but it is not deleting anything. How can I delete the data according to some field.

8
  • Make sure your collection contains at least an object.
    – Marius
    Commented Feb 13, 2014 at 7:14
  • What do you mean it's deleting the whole table?
    – Moose
    Commented Feb 13, 2014 at 7:14
  • @Marius my collection contains only an array.
    – Waseem
    Commented Feb 13, 2014 at 7:15
  • @moose whole table means the complete table data not the structure
    – Waseem
    Commented Feb 13, 2014 at 7:17
  • So you'd like to basically just truncate the table?
    – Moose
    Commented Feb 13, 2014 at 7:17

1 Answer 1

1

Your addFieldToFilter method is incorrect, you need to type:

$categoryModel= Mage::getModel('blog/category')
    ->getCollection()
    ->addFieldToFilter('article_id', array('eq'=>$articleId));  
    foreach ($categoryModel as $cat) {
            $cat->delete();
    }

Or as an alternative, you could go the direct SQL route:

public function delete_articles($article_id_value){
    $connection = $this->_getConnection('core_write');
    $table = $this->_getTableName('my_table_name');
    $sql = 'DELETE FROM ' . $table . ' WHERE `article_id` = ?';
    $connection->query($sql, $article_id_value);
}

public function _getConnection($type = 'core_read'){
    return Mage::getSingleton('core/resource')->getConnection($type);
}

public function _getTableName($tableName){
    return Mage::getSingleton('core/resource')->getTableName($tableName);
}
8
  • your first method is not producing any thing the result is same but i didn't try the second one let me try that option too.
    – Waseem
    Commented Feb 13, 2014 at 7:27
  • And you're sure that there are values in your table with 'any_id' as the article_id ? (echo count($categoryModel); after the selection before the foreach)
    – Moose
    Commented Feb 13, 2014 at 7:27
  • yeah ofcourse definitely.
    – Waseem
    Commented Feb 13, 2014 at 7:33
  • What does the count value say after doing the selection?
    – Moose
    Commented Feb 13, 2014 at 7:35
  • I already said it in the comment above. I didn't understand also, it is showing 0,
    – Waseem
    Commented Feb 13, 2014 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.