1

phtml

<form id="Upload" action="<?php echo Mage::getUrl('followup/index/follow') ?>" method="post"
      enctype="multipart/form-data">
    <input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>"/>
    <p><input id="customer_name" type="hidden" name="customer_name" value="<?php echo $customer->getName(); ?>"></p>
    <p><input id="customer_email" type="hidden" name="customer_email" value="<?php echo $customer->getEmail(); ?>"></p>
    <p><input id="seller_id" type="hidden" name="seller_id" value="<?php echo $id; ?>"></p>
    <p><input id="seller_email" type="hidden" name="seller_email" value="<?php echo $customerData->getEmail(); ?>"></p>
    <p><input id="button" type="submit" name="submit" value="Follow me!"></p>
</form>

controller

public function followAction(){
    $cus_name = ''.$this->getRequest()->getPost('customer_name');
    $cus_email = ''.$this->getRequest()->getPost('customer_email');
    $seller_id = ''.$this->getRequest()->getPost('seller_id');
    $seller_email = ''.$this->getRequest()->getPost('seller_email');

    $collection = Mage::getModel('followup/followup');
    $collection ->setData('customer_name',$cus_name);
    $collection ->setData('customer_email',$cus_email);
    $collection ->setData('seller_id',$seller_id);
    $collection ->setData('seller_email',$seller_email);
    $collection->save();
}

It's insert database successfully but I want to delete if already exist ? How to write controller on that?

4
  • Yes @QaisarSatti
    – user38506
    Apr 12, 2016 at 6:11
  • if Seller id & Seller email & Customer Email are same then only it delete
    – user38506
    Apr 12, 2016 at 6:12
  • if no data is found then add new that your requirement.? Apr 12, 2016 at 6:19
  • Yes @QaisarSatti you are correct
    – user38506
    Apr 12, 2016 at 6:20

3 Answers 3

1

Follow this:

public function followAction() {

    $cus_name = $this->getRequest()->getPost('customer_name');
    $cus_email = $this->getRequest()->getPost('customer_email');
    $seller_id = $this->getRequest()->getPost('seller_id');
    $seller_email = $this->getRequest()->getPost('seller_email');

    $checkcollection = Mage::getModel('followup/followup')->getCollection();
    $checkcollection->addFieldToFilter('customer_email',$cus_email);
    $checkcollection->addFieldToFilter('seller_id',$seller_id);
    $checkcollection->addFieldToFilter('seller_email',$seller_email);

    if (count($checkcollection)) {

        foreach ($checkcollection as $kcollection) {
            Mage::getModel('followup/followup')->load($kcollection->getFollowupId())->delete();
        }
    } 

    // Save new data anyway
    $collection = Mage::getModel('followup/followup');
    $collection ->setData('customer_name',$cus_name);
    $collection ->setData('customer_email',$cus_email);
    $collection ->setData('seller_id',$seller_id);
    $collection ->setData('seller_email',$seller_email);

    $collection->save();
}

for from button

  <form id="Upload" action="<?php echo Mage::getUrl('followup/index/follow') ?>" method="post"
      enctype="multipart/form-data">
    <input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>"/>
    <p><input id="customer_name" type="hidden" name="customer_name" value="<?php echo $customer->getName(); ?>"></p>
    <p><input id="customer_email" type="hidden" name="customer_email" value="<?php echo $customer->getEmail(); ?>"></p>
    <p><input id="seller_id" type="hidden" name="seller_id" value="<?php echo $id; ?>"></p>
    <p><input id="seller_email" type="hidden" name="seller_email" value="<?php echo $customerData->getEmail(); ?>"></p>
    <p><input id="button" type="submit" name="submit" value="Follow me!"></p>
</form>

use that code

<?php 
$checkcollection = Mage::getModel('followup/followup')->getCollection();
    $checkcollection->addFieldToFilter('customer_email',$customer->getEmail());
    $checkcollection->addFieldToFilter('seller_id',$id);
    $checkcollection->addFieldToFilter('seller_email',$customerData->getEmail());

    if (count($checkcollection)) {
        //for exiting record add you button code here
    } else { //for not exiting record add you button code here  
    } ?>
28
  • Can you please explain this part $ids=$checkcollection->getAllIds(); foreach($ids as $id){ Mage::getModel('followup/followup')->load($id)->delete();
    – user38506
    Apr 12, 2016 at 6:29
  • this will return current ids of collection because for deleting the record we need the ids so i used this logic.. Apr 12, 2016 at 6:31
  • Ya it's working but if already available in DB i need a different button if not there i need a different button how to achive this
    – user38506
    Apr 13, 2016 at 7:44
  • where you want to show that button can you add that code? Apr 13, 2016 at 7:47
  • In frontend i already added that code check my question
    – user38506
    Apr 13, 2016 at 7:54
0

Controller code as below:

public function followAction(){
        $cus_name = ''.$this->getRequest()->getPost('customer_name');
        $cus_email = ''.$this->getRequest()->getPost('customer_email');
        $seller_id = ''.$this->getRequest()->getPost('seller_id');
        $seller_email = ''.$this->getRequest()->getPost('seller_email');


        $model = Mage::getModel('followup/followup');
        $collection = $model->getCollection();

        //you don't need to filter multiple field you can only filter your collection using primary key 

        $collection ->addFieldToFilter('customer_name',$cus_name);
        $collection ->addFieldToFilter('customer_email',$cus_email);
        $collection ->addFieldToFilter('seller_id',$seller_id);
        $collection ->addFieldToFilter('seller_email',$seller_email);

        if(count($collection)){
           foreach($collection as $child){
                $model->load($child->getId())->delete();
           }else{
                $model->load(null);
                $model->setData('customer_name',$cus_name);
                $model->setData('customer_email',$cus_email);
                $model->setData('seller_id',$seller_id);
                $model->setData('seller_email',$seller_email);
                $model->save();
           }
        }
}
0
0

if you will use this code

if (count($checkcollection)) {
    $ids = $checkcollection->getAllIds();
    foreach ($ids as $id) {
        Mage::getModel('followup/followup')->load($id)->delete();
    }
} else {

    $collection = Mage::getModel('followup/followup');
    $collection ->setData('customer_name',$cus_name);
    $collection ->setData('customer_email',$cus_email);
    $collection ->setData('seller_id',$seller_id);
    $collection ->setData('seller_email',$seller_email);

    $collection->save();
}

Then records will be deleted, but new record will not be created. If you need just to delete before saving new record - there should be no ELSE case. Like this:

if (count($checkcollection)) {
    $ids = $checkcollection->getAllIds();
    foreach ($ids as $id) {
        Mage::getModel('followup/followup')->load($id)->delete();
    }
}

$collection = Mage::getModel('followup/followup');
$collection ->setData('customer_name',$cus_name);
$collection ->setData('customer_email',$cus_email);
$collection ->setData('seller_id',$seller_id);
$collection ->setData('seller_email',$seller_email);
$collection->save();

OR - much better - just to "do nothing" if record exists.

  if (!count($checkcollection)) {
        Mage::getModel('followup/followup')->setData(
                             array(
                                'customer_name' => $cus_name,
                                'customer_email' => $cus_email,
                                'seller_id' => $seller_id,
                                'seller_email' => $seller_email
                             )
                       )->save();
    }

Your Answer

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