2

i am trying to delete record from database is there any opposite methode of setdata to delete record from DB

$postModel = Mage::getModel('brainvire_brand/post');
$image=$data['image'];
$bname=$data['bname'];
$content=$data['content'];
$is_active=$data['is_active'];

$postModel->setBname($bname);
if($data['imagedelete'])
{
      $postModel->setImage('');
}
else
{
      $postModel->setImage($image);
}
$postModel->setContent($content);
$postModel->setIs_active($is_active);
  • Plz do not suggest me to add blank data – Vaibhav Ahalpara Mar 25 '15 at 11:22
  • vaibhav,is flat table i.e single table model? ... not a eav tbale – Amit Bera Mar 25 '15 at 13:12
1

First,i Guss you have using flat stucture

Better way to delete there any model by function delete(); By this you can delete model one by one.

$postModel = Mage::getModel('brainvire_brand/post');
$postModel->load($PkTablevalue);

$postModel->delete();

Example. For delete an product you can use:

$productObject = Mage::getModel('catalog/product');
$productObject->load($PID);
$productObject->delete();

According you comment you can unset a field value by unsetData('index')

just: $postModel->unsetData('image'); then $postModel->save();

  • i just want to delete one attribute value like i just want to delete value of image – Vaibhav Ahalpara Mar 25 '15 at 13:25
1

is there any opposite method of setData to delete record from DB.

No, I don't believe you can delete EAV table records using any Magento or magic methods. However, doing setImage(null) will effectively achieve what you want (removing image data, but not the actual DB record).

1

There is unsetData() which unsets the attribute in your PHP model but this results in the attribute just being ignored during save. To delete the attribute value, you need to set it to a value that is considered empty. What this can be depends on the backend type, but "null" always works.

Take a look at Mage_Eav_Model_Entity_Attribute_Abstract::isValueEmpty():

public function isValueEmpty($value)
{
    $attrType = $this->getBackend()->getType();
    $isEmpty = is_array($value)
        || ($value === null)
        || $value === false && $attrType != 'int'
        || $value === '' && ($attrType == 'int' || $attrType == 'decimal' || $attrType == 'datetime');

    return $isEmpty;
}

I assume, "image" has the type 'varchar', so setImage('') will just set the value to an empty string, but setImage(null) will actually remove it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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