ivn
ivn

Reputation: 1275

Update Magento database table with a WHERE clause

I have a table called 'sample' with fields as 'sample_id, 'order_id', order_email_id', 'review_request', 'coupon_sent'.

$to_date = date('Y-m-d H:i:s',strtotime('-1 days'));
        $orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', 'complete')->addFieldToFilter('updated_at',array('to' => $to_date ))->addAttributeToSelect('customer_email')->addAttributeToSelect('entity_id');
        foreach ($orders as $order) 
        {
            $email = $order->getCustomerEmail();
            $id = $order->getEntityId();
            //echo 'Email: ' . $email . ' Id: ' .$id .'<br/>' ;     
            $sample = Mage::getModel('sample/sample');
            $sample->setOrderId($id);
            $sample->setOrderEmailId($email);
            $sample->save();
            echo '<br/>Record Added';

        }

        $posts = Mage::getModel('sample/sample')->getCollection();
        foreach($posts as $sample_post)
        {
            if($sample_post->getReviewRequest()==0)
            {
                     code to send email to email id's from this table's order_email_id field
                    try
                    {
                       (if($mail->send() == true)
           { 
            echo "<br> Mail Sent ";
                       //**WHERE clause condition to update 'review_request' field for all successful emails sent**

                      }
                      else {echo 'Mail not Sent';}
                   }
                   catch(Exception e) {}

So acc to my code how do I put the WHERE clause which sets review_request = 1 for all the records where the emails have been sent successfully.

Thanks in advance

Upvotes: 1

Views: 3109

Answers (2)

sukarna
sukarna

Reputation: 21

This doesn't work for me:

$model1->load()->addFieldToFilter('order_email_id',array('eq' => $toEmail))->getSelect();

while this works:

$model1->addFieldToFilter('order_email_id',array('eq' => $toEmail))->load();

Upvotes: 2

ivn
ivn

Reputation: 1275

Thanks @clockworkgeek for bringing that link again and downvoting which 'probed' me even further to work out the solution. :P I think I found it.

 if($mail->send() == true)
 { 
     echo "<br> Mail Sent ";
 $model1 = Mage::getModel('sample/sample')->getCollection();
            $model1->load()->addFieldToFilter('order_email_id',array('eq' => $toEmail))->getSelect());
foreach($model1 as $final_model)
{ 
       $final_model->setReviewRequest(1)->save();
    }
 }

And this perfectly updates my table with all the records where the emails were sent with review_request = 1 Thanks.

Upvotes: 3

Related Questions